Search code examples
apache.htaccessmod-rewriteurl-rewritingfriendly-url

Pretty RewriteRule back to old format


I have some old urls indexed by Google in format https://example.com/1234/name-of-page Because I don't use these RewriteRules anymore I would like re-route them to https://example.com?id=1234&name=name-of-page

How can I do this using .htaccess?

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]


RewriteRule ^([^/]*)/(.*)/?$ https://%{HTTP_HOST}/title.php?id=$1&name=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d


RewriteRule ^sitemap\.xml/?$ sitemap.php

Solution

  • If you are looking specifically for uri which starts from 1234/name-of-page to be rewritten then try following. Please make sure you clear your browser cache before testing your URLs.

    RewriteEngine ON
    RewriteRule ^(1234)/(name-of-page)/?$ title.php?id=$1&name=$2 [L]
    

    For more generic rules you could try following. Please make sure you try either above OR following rules only in your .htaccess file at a time.

    RewriteEngine ON
    RewriteRule ^([^/]*)/(.*)/?$ title.php?id=$1&name=$2 [L]