Question

we got stuck with an url rewrite and redirection 301 in .htacces

At the moment our Internetsite has only got one file (index.php) which get passed as variable the Category ID to then show the content of that category.

For example: the Category Nr 4 gets called with the link www.oursite.com/index.php?CATID=4 which shows the content of the category "map-france" the Category Nr 5 gets called with the link www.oursite.com/index.php?CATID=5 which shows the content of the category "map-italy"

Alltogether we have ca 800 Categories.

We would like to rewrite the urls so that the category names will be displayed: For example: www.oursite.com/map-france instead of www.oursite.com/index.php?CATID=4 www.oursite.com/map-italy instead of www.oursite.com/index.php?CATID=5

The best idea at the moment is to use the mod_rewrite functions in .htacces

We have managed so far to achieve this with the following code:

RewriteEngine On
RewriteRule    ^map-france/?$    index.php?CATID=4    [NC,L]
RewriteRule    ^map-italy/?$    index.php?CATID=5    [NC,L]

This works pefectly.

To avoid double indexing from search engines we should also apply a redirect 301 from the old urls to the new ones. We managed to do that with the following code:

RewriteCond %{QUERY_STRING} CATID=4
RewriteRule ^index\.php$  http://www.oursite.com/map-france/? [L,R=301]

RewriteCond %{QUERY_STRING} CATID=5
RewriteRule ^index\.php$  http://www.oursite.com/map-italy/? [L,R=301]

If we put everything together it does not work and it seems to generate a never ending redirection loop

What are we doing wrong?

Thanks a lot for your help :)

Was it helpful?

Solution

Try matching agaist the request instead of the %{QUERY_STRING}

RewriteCond %{THE_REQUEST} \?CATID=4
RewriteRule ^index\.php$  http://www.oursite.com/map-france/? [L,R=301]

RewriteCond %{THE_REQUEST} \?CATID=5
RewriteRule ^index\.php$  http://www.oursite.com/map-italy/? [L,R=301]

The rewrite engine loops until the URI stops changing, that means your first rule adds the query string, and you second rule matches the query string and redirects to without the query string. Then the browser makes the same request again, and your first rule gets applied, then the second rule matches against the query string and redirects the browser again, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top