1

I imagine this is a problem with a fairly simple solution to anyone who's proficient with the syntax of .htaccess files, but unfortunately that I am not.

I've worked out how to make it so that when someone accesses domain.com/something/5 then internally it acts as though they accessed domain.com/something/?page=5. However, as it is, the user can still access the "ugly" URL if they choose to, and apparently it's bad SEO-wise to have two different URLs with exactly the same content. So, I'd like to force users to use the clean URL.

Here's my .htaccess file:

RewriteEngine On
RewriteRule ^\?page=([0-9]+)$ index.php?page=$1 [R=301,NC,L]
RewriteRule ^([0-9]+)/?$ index.php?page=$1 [NC,L]

Of course, the bottom isn't really relevant to the question as it's the rule that internally redirects clean URLs to the dirty ones, but I thought I'd better include it anyway.

I've gone over it probably a dozen times searching for a mistake in the regex, as well as just hopefully removing things like the caret and the Dollar sign, but still nothing happens when I access it via ?page=5.

Any ideas on how I can do this?

  • possible duplicate of [How can I match query string variables with mod\_rewrite?](http://stackoverflow.com/questions/2252238/how-can-i-match-query-string-variables-with-mod-rewrite) – showdev Apr 09 '15 at 00:07

1 Answers1

1

You can't access Query strings like that in the RewriteRule. If you don't want them to access the "ugly" URL still then you need to check the REQUEST and redirect them to the SEF URL.

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\&\ ]+)
RewriteRule ^ /%1? [R=301,L]

RewriteRule ^([0-9]+)/?$ index.php?page=$1 [NC,L]

Let me know if the answer solves your issue.

Panama Jack
  • 24,158
  • 10
  • 63
  • 95
  • Thanks for the response, but unfortunately nothing happens when I try this. Also, do you mind explaining a bit what that does? I understand the gist of it, but I'm a bit confused about why the `^[A-Z]{3,9}\` part is in there. –  Apr 09 '15 at 15:53
  • Clear your browser cache or try in another browser. The regex means it can be any upper case character from A-Z and between 3 and 9 in length. The reason for this is THE_REQUEST variable provides the type of request it was e.g. `POST, GET. HEAD` etc. so that just covers any request that is sent. – Panama Jack Apr 09 '15 at 15:58