Mobile app version of vmapp.org
Login or Join
Rivera981

: Changing from parameter to readable URLs 301 redirect I've recently changed my URLs with parameters to more readable URLs with the following .htaccess: RewriteRule ^page/([A-Za-z0-9-+_s]+)$ /index.php?n=

@Rivera981

Posted in: #Htaccess #Redirects #Seo

I've recently changed my URLs with parameters to more readable URLs with the following .htaccess:

RewriteRule ^page/([A-Za-z0-9-+_s]+)$ /index.php?n= [L]
RewriteRule ^category/([A-Za-z0-9-+_s]+)/([0-9]+)$ /index.php?p=&b= [L]


I believe this poses problems now with duplicate content on the search engines, as index.php URLs and domain.com/page/ URLs will both be indexed.

If calls made to domain.com/page/1 get redirected to index.php?n=1 how can I redirect requests made to index.php to domain.com/page/ ?

My attempt to do so is giving me a 404 code sending me to domain2.
#RewriteCond %{QUERY_STRING} ^n=([A-Za-z0-9]+)$ [NC] #RewriteRule ^index.php$ domain.com/page/? [R=301,L]



.htaccess:

IndexIgnore *
ErrorDocument 404 domain2.com
#REDIRECTS
Redirect 301 /keyword.php domain.com Redirect 301 /inc/keyword.php domain.com



Options +FollowSymlinks
RewriteEngine on

#Redirect all pages in a folder to a single URL
RewriteRule ^test/(.*)$ domain.com [L,R=301]

# HTTP HOST, escape . and start string with ^
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ domain.com/ [R=301,NC]


RewriteCond %{QUERY_STRING} ^p=h&m=([A-Za-z]+)&y=([0-9]+)$ [NC]
RewriteRule ^index.php$ domain.com? [R=301,L]


#RewriteCond %{QUERY_STRING} ^n=([A-Za-z0-9]+)$ [NC]
#RewriteRule ^index.php$ domain.com/page/? [R=301,L]


RewriteRule ^page/([A-Za-z0-9-+_s]+)$ /index.php?n= [L]

RewriteRule ^category/([A-Za-z0-9-+_s]+)/([0-9]+)$ /index.php?p=&b= [L]

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Rivera981

2 Comments

Sorted by latest first Latest Oldest Best

 

@Cofer257

Can you use a proxy in front of your Apache? Something like Varnish can do the rewriting for you, and as you separate concerns, you lower risk of building redirect loops and the likes. And that will speed up your website...

If you can't run a proxy on your server, maybe you can consider using external services than can do it for you, plus other things. The service I'm running does, and I think does it well. See the URL in my profile.

10% popularity Vote Up Vote Down


 

@Ogunnowo487

You are right to think about duplicate content when you are changing a URL structure and the old URLs are already indexed.

# My attempt to do so is giving me a 404 code sending me to domain2. #RewriteCond %{QUERY_STRING} ^n=([A-Za-z0-9]+)$ [NC] #RewriteRule ^index.php$ domain.com/page/? [R=301,L]


In that case domain.com/page/ would seem to be an invalid URL, but you are also removing the information contained in the query string and not passing it on to the "user friendly" URL (so all URLs are being redirected to the same invalid URL).

You also need to be careful of redirect loops, since you are internally rewriting to a URL that contains the query string (for your user friendly URL to work) but also need to redirect URLs that contain the query string to prevent duplicate content.

We can get around the redirect loop by checking against %{THE_REQUEST} which holds the full HTTP request as sent from the client and won't change after a rewrite.

So, for your first "page" URLs:

RewriteCond %{THE_REQUEST} ?n=([A-Za-z0-9]+)$
RewriteRule ^index.php$ /page/%1? [R=301,L]


Note that the pattern [A-Za-z0-9] (taken from your example) in the URL is more restrictive than the pattern in your rewrites [A-Za-z0-9-+_s] (which allows spaces!?) - both patterns should be the same, and not allow spaces. (Note that THE_REQUEST contains spaces, so the regex will need to change if you really really need this.)

EDIT: In view of the update below (to allow two parameters), I have added a $ (position marker) in the above RewriteCond directive to signify the end of the string. This now only allows one URL parameter.

And the external redirects should come before the internal rewrites (which you appear to have done anyway).

UPDATE: For a second parameter (as mentioned in comments), add these additional directives:

RewriteCond %{THE_REQUEST} ?n=([A-Za-z0-9]+)&y=([A-Za-z0-9]+)$
RewriteRule ^index.php$ /page/%1/%2? [R=301,L]


This checks for exactly two parameters (n and y in that order) and redirects accordingly.

(Note the edit to the first rule above for one parameter.)

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme