skip to Main Content

I have a website in php which has some pages:

www.example.com/news
www.example.com/people
www.example.com/music

On each of these pages is an overview of content. If you click, for example, on a link on www.example.com/news, an article opens with that url using a template (www.example.com/article.php?url='example-of-article-url')

For each of these pages there is a rewrite needed. The displayed url should be www.example.com/news/example-of-article-url
and this should acces the article.php file. The news directory is virtual. The www.example.com/news, www.example.com/people, www.example.com/music are being rewritten by this code:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

What I have been trying is:

RewriteRule ^news/(.*) article.php?url='$1' [R=301,L]

However, when this is loaded, css and images aren’t. The content is generated thinking it’s in the /news/ directory, thus unable to find the other files. I can’t use canonical URL’s because of SEO-reasons.

2

Answers


  1. First of all dont forget to add this at the very start:

    RewriteEngine On
    

    Remove the Qoutes from ‘$1’ your last line and try the method below instead.

    RewriteEngine On
    RewriteRule ^news/(.*)$ article.php?url=$1 [R=301,L]
    

    To redirect with the contents of a whole directory to your location:

    RewriteEngine On
    RewriteRule ^news/(.*)$ article.php?url=$1 [R=301,NC,L]
    

    Or you can do it this way too with a Slash on the Start:

    Redirect the whole folder (news) to your new location:

    RewriteEngine On
    RewriteRule ^/?news/(.*)$ article.php?url=$1 [R=301,L]
    

    or

    RewriteEngine On
    RewriteRule ^/news/(.*)$ article.php?url=$1 [R=301,L]
    

    To redirect with the contents of a whole directory to your location:

    RewriteEngine On
    RewriteRule ^/news/(.*)$ article.php?url=$1 [R=301,NC,L]
    

    Best of Luck

    Login or Signup to reply.
  2. You should add:

    <base href="/"/>
    

    In your <head>section.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search