0

So far, I have made my .htaccess to let me remove the ".php" extension, but that isn't enough for me. I want to be so that example.com/test?id=asdfjK could be able to be accessed as example.com/asdfjK. So that it accepts only the main php get argument in the URL (I don't know what to call them.

Here is my .htaccess file so far:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_]+)$ /image.php?ID=$1
RewriteRule ^([a-zA-Z0-9_]+)/$ /image.php?ID=$1
Adam S.
  • 305
  • 1
  • 3
  • 14
  • Does anyone that is on right now know about how to do this? I've read other questions, but the answers haven't helped me. – Adam S. Jan 20 '14 at 06:30

2 Answers2

1

There's no way to differentiate between what gets sent to index.php and what gets sent to image.php. The patterns are identical, which means everything will match the first one and nothing will get routed to image.php. You've got to add something to the url so that you can match against it. Something like:

http://example.com/image/abcdefg123456

And that means the htaccess file would look something like:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?page=$1 [L,QSA]
RewriteRule ^image/([a-zA-Z0-9]+)/?$ image.php?page=$1 [L,QSA]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Now when I go to my domain and use /image/sadfklj9 (random code), Chrome loads for a second or two then tells me it runs a redirect loop. – Adam S. Jan 20 '14 at 06:45
0

The PHP arguments after the question mark are called the "query string".

Try something like this:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^([a-zA-Z0-9]+)$ /index.php?page=$1 [L]

This should redirect anything from:

http://example.com/abc123

To:

http://example.com/index.php?page=abc123

See also:

Community
  • 1
  • 1
Anthop
  • 138
  • 8
  • That didn't work :(. The examples just gives me the same error as in my previous comment on the answer above. – Adam S. Jan 20 '14 at 06:45
  • I think you can get around the infinite redirect by putting in a RewriteCond above your rule. Basically, I think because the rule matches the rewritten output, it will rewrite it again (and again, and so forth). A condition prevents that by only applying the rule if it hasn't already been rewritten. See the updated answer above. – Anthop Jan 20 '14 at 07:34