views:

61

answers:

2

Currently my .htaccess looks like this...

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

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


RewriteRule ^(.*)$ $1.php [L,QSA]  

It currently changes any /xxx.php file into /xxx. This is great for SEO. However, I also want Mr. htaccess to convert certain URLs into a URL + query string. For instance when user goes to

/specific/somerandominfo

Then somerandominfo is passed to the specific.php file. I normally have no problem doing this using rewrites, but because of my fancy catchall rewrite, I can't figure out how to do it.

For example if I add

RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC]  

to my .htaccess, then hitting up /specific/somerandominfo just serves me a big fat 500 Internal Service Error.

Any help from you apache gurus out there would be so, so cool.

Thanks!

p.s. anybody want to also throw in any other cool SEO tricks that they like? I'll bake you cookies.

A: 
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC]

This is mostly correct. I'd just add the B flag, like this:

RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC,B]

This causes the capture group $1 to be properly escaped for use in query strings. Note that you can still use QSA to retain the query parameters used in the original request (in addition to somerandominfo).

Perhaps you'll want to post your actual RewriteRule.

Artefacto
Hi Arte, the actual rule I was using looks like...RewriteRule ^workouts/([^/]+)$ /workouts.php?id=$1 [NC]When I attempt to access /workouts/8, for instance, I do receive a 500.Thank you so much for your help!
Dylan
A: 

You are getting 500 error because your rules are creating an infinite cycle. Check apache error log to see if it is true. So you should design your rules properly. Maybe like that:

RewriteRule ^([^/]*)$ $1.php [L]
RewriteRule ^(.*)/(.*)$ $1.php?var=$2 [L]
ehpc
The rules he posted could not create an infinity cycle. Though it's possible this is case (he didn't post the real rule he used).
Artefacto
Actually they are. I've tested them on my xampp.
ehpc
Hey there, the rule I posted is pretty much a CC of the actual rule I'm using. I just changed the .php filename from "workouts" to specific and the $_GET variable from "id" to "somerandominfo" :)Also, ehpc, does this create a case in which ANY file I go to will be treated as a var? For instance, if I have a folder called test and in that folder is first.php, will this set of Rewrites try to treat /test/first an attempt to send var=first to test.php? Sorry if that was a convoluted question. :)Thank you for your help!
Dylan
Dylan, no. If you have a dir or file corresponding to address, the server will get the actual file ommiting htaccess rules. (But in your case if there is a dir 'test' and there is no file 'first' in that dir, the server will give 500 error) This is possible because you specified such behaviour by RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d . So if you do not want to dir or file to be served, just remove these lines from your htaccess.
ehpc
ehpc, you're awesome! Thanks
Dylan
You're welcome :)
ehpc