Reputation: 7625
This would seem to be a simple .htaccess RewriteRule, yet I'm still having issues with it.
Basically, I'd like any text after the domain to be interpreted as index.php?$text
, unless of course, that text is a filename.
Example:
http://domain.com/register
-> http://domain.com/index.php?register
http://domain.com/images/image.jpeg
-> http://domain.com/images/image.jpeg
This is what I have so far:
RewriteRule ^[A-Za-z]+$ index.php?$1
However, it's not passing the $_GET variable to the PHP page as it should.
Thanks for any help.
EDIT I'm using PHP- so if there is a better scripting method of going about this than .htaccess, let me know.
Upvotes: 0
Views: 772
Reputation: 634
First off, your never grouping (storing) the text so $1 is "". You should use:
RewriteRule ^([A-Za-z]+)$ index.php?$1
Second, if you want to ignore filenames (anything with a '.' in it). Use:
RewriteCond %{REQUEST_URI} "^([^\.]*)$"
RewriteRule ^([.]+)$ index.php?$1
Which says if the path after the domain does not contain a '.' rewrite it as index.php?foo
If you would like to limit the filename extensions instead, use:
RewriteCond %{REQUEST_URI} "^([^\.(php|jpeg|html|css|ico)]*)$"
RewriteRule ^([.]+)$ index.php?$1
Upvotes: 1
Reputation: 1926
Add the QSA flag to your rewrite rule so it would look something like
RewriteRule ^[A-Za-z]+$ index.php?$1 [QSA]
QSA (query string addition) adds the query string onto your rewrite results. so any $_GET's will get appended onto what gets passed to your php script.
You might think about adding the L flag too, especially if thats all your trying to do with the rewrite. It tells the processor that if this rule processes its the end of the rewrite.
So it would look like
RewriteRule ^[A-Za-z]+$ index.php?$1 [QSA,L]
I often add NC as well, which makes it case insensitive so you don't have to handle both upper and lower case like you're doing in your regex.
Another thing to keep in mind is that you're not passing $1 as a proper get var. it would be better to name the var that its coming in on. And I just noticed that you're not creating the $1 var in the first place. Try this.
RewriteRule ^([A-Za-z]+)$ index.php?urlRequest=$1 [QSA,L]
Then check it in your index.php
echo $_GET['urlRequest'];
Hope that helps!
Upvotes: 1