Home > Back-end >  Rewrite .php URL with two parameters and redirect old .php URL to new .htm URL
Rewrite .php URL with two parameters and redirect old .php URL to new .htm URL

Time:06-26

I've set RewriteRules in my htaccess file to rewrite my php URLs to SEO friendly .htm ones. Now I have to set 301 Redirect to point the php URLs to the new htm URLs, but cannot find a solution for this. As you maybe guess, I'm not familiar with the rewrite/redirect rules :( In my htaccess I have this for both the accessory.php and the accessory-details.php files and it works perfect.

RewriteRule accessories-(.*)\.htm$ accessories.php?lang=$1
RewriteRule accessory-details-id-(.*)-(.*)\.htm$ accessory-details.php?id=$1&lang=$2

The problem is I still can access the php URLs. The php URL have good rankings, so I need this 301 redirect for SEO purposes. How can I do a rewrite and 301 redirect? Note that some files have one and other files have two GET parameters. I couldn't set a working code myself and this makes me mad. These are only two examples, there are many similar "pairs" of files on my root, but I think I could manage to adapt the code to the other files, if I could get these two working.

CodePudding user response:

The 'id' parameter is an integer - 1 2, 3, ..., the 'lang' parameter is a string - "en" or "fr". For example: 'accessory-details.php?id=6&lang=en' (old URL with parameters) and 'accessory-details-id-6-en.htm' // new SEO friendly URL

Your existing regex (subpatterns) are too generic. .* matches anything (including the hyphen, which you are using as a delimiter) so results in the regex being rather ambiguous. The regex should be as specific as required to ensure you are not unnecessarily rewriting (and processing) URLs which are invalid from the outset.

Assuming your .htaccess file and other files are in the document root then try the following instead:

Options -MultiViews

RewriteEngine On

# Redirect old URLs (PHP files) to new URLs
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
RewriteRule ^accessories\.php$ /accessories-%1.htm [QSD,R=301,L]

RewriteCond %{QUERY_STRING} ^id=(\d )&lang=([a-z]{2})$
RewriteRule ^accessory-details\.php$ /accessory-details-%1-%2.htm [QSD,R=301,L]

# Internal rewrites (new URL to file)
RewriteRule ^accessories-([a-z]{2})\.htm$ accessories.php?lang=$1 [END]
RewriteRule ^accessory-details-id-(\d )-([a-z]{2})\.htm$ accessory-details.php?id=$1&lang=$2 [END]

The END flag (Apache 2.4) ensures that no further processing occurs after the URL is rewritten - so the rewritten URL is not redirected (during a second phase of processing, as would happen with the L (last) flag).

These directives also assume that your URLs contain no other URL parameters, other than id and lang as stated. And are consistently in the order as stated. ie. id=123&lang=aa, not lang=aa&id=123.

The %1 and %2 backreferences contain the values captured from the preceding CondPattern.

UPDATE: The QSD (Query String Discard) flag on the external redirects is necessary to discard the original query string from the redirect response.

NB: Test first with 302 (temporary) redirects to avoid potential caching issues. You will likely need to clear your browser cache before testing.

  • Related