combined 301 redirects not working

combined 301 redirects not working - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, 301-redirect, mod-rewrite, user-friendly, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


I made some changes to the files on a website and now Search Console is showing me 404 errors for some URLs.



The normal setup, which works fine is like this:




  • the user-friendly URL



    mywebsite.com/modeles-voiture/Volvo/XC60



is rewritten to get to the PHP script as:



 RewriteCond %REQUEST_URI ^/?modeles-voiture [NC]
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule /([A-Z][-A-Za-z]+)/([-A-Za-z0-9.]+$) /models.php?brand=$1&model=$2 [L]


Now, I do have some old URLs out there, that are generating the 404 errors and that look like:



mywebsite.com/modeles.php?brand=Volvo&model=XC60


So, I came up with the following redirect to send them to the right place.



This first step would redirect the old URLs to the new, user-friendly ones:



RewriteCond %REQUEST_URI /modeles.php [NC]
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule brand=([A-Z][-A-Za-z]+)&model=([-A-Za-z0-9.]+$) http://www.soeezauto.com/modeles-voiture/$1/$2 [R=301,L]


Then, the existing code ( below ), would do its regular job of sending over to the proper PHP script.



So ( just repeating what I stated up on the top ):



 RewriteCond %REQUEST_URI ^/?modeles-voiture [NC]
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule /([A-Z][-A-Za-z]+)/([-A-Za-z0-9.]+$) /models.php?brand=$1&model=$2 [L]


My this is not working. Still getting a 404 error.



I did test all the individual regexps ( at regex101.com ) and they seem to do what they are supposed to.



I may just have made the wrong assumption concerning the chaining, but in that case, what is the solution to this?


Solution :

RewriteCond %REQUEST_URI /modeles.php [NC]
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule brand=([A-Z][-A-Za-z]+)&model=([-A-Za-z0-9.]+$) http://www.soeezauto.com/modeles-voiture/$1/$2 [R=301,L]


You can't match the query string with the RewriteRule pattern. The RewriteRule only matches against the URL-path (less the query string). To match the query string you need to use a RewriteCond directive and check against the QUERY_STRING server variable.



It is also more efficient to match the URL-path in the RewriteRule, rather than using a condition to match against REQUEST_URI (where possible).



Try something like:



RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %QUERY_STRING ^brand=([A-Z][-A-Za-z]+)&model=([-A-Za-z0-9.]+)$
RewriteRule ^modeles.php http://www.example.com/modeles-voiture/%1/%2? [R=301,L]


This matches the exact query string, eg. brand=Volvo&model=XC60 - if there are other URL params then this will not match.



EDIT: The ? on the end of the substitution effectively removes the query string from the redirected URL. Alternatively, on Apache 2.4, you can use the QSD (Query String Discard) flag.



You don't need to escape slashes in Apache config regex. (Apache regex does not use slash delimiters.) In a regex character class you also don't need to escape literal dots, or hyphens if they occur at the start or end of the character class.



The RewriteRule substitution uses %1 and %2 (as opposed to $1, etc) as a backreference to the last captured group(s) in the RewriteCond directive.


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect