Alper Aydingül
Alper Aydingül

Reputation: 261

mod_rewrite configuration does not work

I have a page with urls like this:

http://example.com/index.php?site=contact
http://example.com/index.php?site=about

So I try to create custom urls like

http://example.com/contact-the-person
http://example.com/cityname/about

to avoid duplicate content the first url need a permanent redirect into the new code.

this is my code:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+index\.php?site=contact[\s?] [NC]
RewriteRule ^/contact-the-person [R=301,L]
RewriteRule ^contact-the-person/?$ index.php?site=contact [L,NC]

Update:

I changed my code into

RewriteEngine on 
RewriteRule ^cityname/about$ index.php?site=contact

and it works now. I can open the url with both links

http://example.com/index.php?site=contact
http://example.com/cityname/about

I just need a redirect from the php version to the clean url now, to avoid dublicate content

Upvotes: 1

Views: 43

Answers (1)

Don't Panic
Don't Panic

Reputation: 14520

Get rid of RewriteBase, if your base is / it is redundant and just complicates things. I am not sure what your RewriteCond is doing, but it isn't necessary to do the 2 rewrites you describe in the question, so get rid of it too.

To make /contact-the-person work:

RewriteRule ^/contact-the-person index.php?site=contact [L,NC]

To make /cityname/about work:

RewriteRule ^/cityname/about index.php?site=about [L,NC]

So the complete file:

RewriteEngine On
RewriteRule ^/contact-the-person /index.php?site=contact [L,NC]
RewriteRule ^/cityname/about /index.php?site=about [L,NC]

UPDATE

To also redirect your index.php?site=contact links to the new pretty format, you'll need to do an external redirect, so that the browser actually makes a new request, and the URL in the browser changes. Do that by adding R to the flags. 301 specifies the http response header, and will ensure your link rankings are preserved. For the example you gave, add a new rule:

RewriteRule ^/index.php?site=contact /contact-the-person [L,R=301]

Upvotes: 1

Related Questions