Mobile app version of vmapp.org
Login or Join
Shanna517

: Combined 301 redirects not working 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

@Shanna517

Posted in: #301Redirect #Htaccess #ModRewrite #UserFriendly

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=&model= [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.]+$) www.soeezauto.com/modeles-voiture// [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=&model= [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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Shanna517

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

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.]+$) www.soeezauto.com/modeles-voiture// [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 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 , etc) as a backreference to the last captured group(s) in the RewriteCond directive.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme