2

I have the following in my htaccess file, which takes a URL like: www.example.com/Page.php?id=About and turns it into www.example.com/about. The only issue is that the old URLs are not redirecting to the new URLs when I set up Redirect 301s in the htaccess (which I understand is because you're not supposed to pass parameters in basic Redirect 301s - example of basic Redirect that I've tried below).

Redirect 301 /Page.php?id=About http://www.example.com/about

The catch is that the new URLs don't necessarily just strip out the current id and replace it with a lowercase version. For example, I would also want to:

Redirect 301 /Page.php?id=Example http://www.example.com/example-page

I want to keep the current functionality of rewriting the URLs, but also want any visits to the old URLs to redirect to the new ones (i.e. to update Google index). Visiting www.example.com/Page.php?id=About at the moment still works but shows a page with no content.

Options +FollowSymLinks
RewriteEngine on

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

RewriteRule ^([a-zA-Z0-9\-]+)/?$ Page.php?id=$1
RewriteRule ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ Page.php?id=$1&tab=$2
RewriteRule ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ Page.php?id=$1&tab=$2&tabid=$3
Steve R
  • 95
  • 1
  • 1
  • 8

1 Answers1

7

If you add this rule to your .htaccess it should do the work of the Redirect. It will add the ?id=About to the redirect to /about. If you are running on version 2.4.0 or later, you can add the QSD flag to discard of the query string in the redirect.

RewriteCond %{QUERY_STRING} ^id=About$
RewriteRule ^page.php /about [NC,R,L]
Lars Lind Nilsson
  • 1,136
  • 6
  • 14
  • Thanks for the suggestion. Unfortunately, visiting http://www.example.com/Page.php?id=About isn't redirecting to http://www.example.com/about though? – Steve R Dec 18 '13 at 13:10
  • The rule was for page.php with lower-case p. I have just updated the answer with a NC flag to make the rule case-insensitive. Now it should match both page.php and Page.php – Lars Lind Nilsson Dec 18 '13 at 13:16
  • Thanks, nearly there, I'm getting: http://www.example.com/about?id=About now. Is it possible not to have the ?id=About at the end? – Steve R Dec 18 '13 at 13:21
  • 4
    @SteveR: Make last line as: `RewriteRule ^page.php /about? [NC,R,L]` – anubhava Dec 18 '13 at 13:23
  • @anubhava Wouldn't the QSD flag (if using version 2.4.0+) also remove the query string (don't have a 2.4 to test on) – Lars Lind Nilsson Dec 18 '13 at 13:36
  • Yes QSD does the same but as you noted required newer Apache – anubhava Dec 18 '13 at 13:38
  • How can we achieve the same redirect using RedirectMatch rule? I wish to redirect some pdf files but my original URL's may have parameters so i wish to check for parameters but in RedirectMatch. – codingbbq Apr 23 '14 at 10:31
  • I'm quite sure RedirectMatch doesn't look at parameters. But something like this can perhaps do the same with RewriteRule: RewriteCond %{QUERY_STRING} (.*)xyz=([^&]*)(.*) RewriteRule one.php /two.php?%1abc%2%3 [R] -- This should redirect from one.php to two.php and rename the xyz parameter to abc. – Lars Lind Nilsson Apr 30 '14 at 13:56
  • is there any way to pass forward legitimate parameters?? something like `RewriteRule ^/?$ "https\:\/\/example\.com\/?par=value" [R=301,L]`, which in actuality seems to ignore the parameters?? – oldboy Aug 07 '19 at 01:04