tags:

views:

308

answers:

3
Options +FollowSymlinks
RewriteEngine On
RewriteBase /

##RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.(js|ico|gif|jpg|png|css)$ [NC]
RewriteCond %{REQUEST_FILENAME} !^index.php [NC]
RewriteCond %{REQUEST_FILENAME} !^$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d

## RewriteRule ^/([a-zA-Z_+-]+).php$  index.php?p=%1 [R=301]
## RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?p=$1 [R=301,L]
## RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php?b={REQUEST_FILENAME}
RewriteRule * index.php?p={REQUEST_FILENAME} [R=301,L]

Above you can see my attempts to redirect any request that is not an existing directory, is not index.php and is not a static resource to redirect to index.php?p=resourcename

I am not having any luck. Basically the purpose of this is to redirect static and old urls to new ones as I have just rewritten an old site.

The PHP will handle the redirect logic.

At the moment this code causes an internal server error, I assume because it is caught in a redirect loop. What have I done wrong? My brain is fried after a long day.

A: 

You forgot the % in front of the variable (%{REQUEST_FILENAME}) and an expression that should be repeated zero or more times (* is just a quantifier):

RewriteRule .* index.php?p=%{REQUEST_FILENAME} [R=301,L]
Gumbo
Thanks, corrected that now. It's still broken though...
Antony Carthy
+1  A: 

Untested, but worth a try :

RewriteRule .* /index.php?p={REQUEST_FILENAME} [R=301,L]

The ".*" part means you want to match 1 or more characters (any of them). And the "/" in front of the "index.php" is probably not mandatory but makes things clearer even if you have the RewriteBase option set to "/" already.

You may also want to add the parameter "QS" between the brackets, to be sure to get the querystring that may be passed with the queries (would be [QS,R=301,L]). Hope this works, and this helps :)

Edit: There's also the "%" in front of "{REQUEST_FILENAME}", as stated by Gumbo.

Nicolas
The flag to pass along the query string is `QSA`
jason
Indeed it is. My bad :)
Nicolas
A: 

This code eventually solved my problem.

Thanks for the help, though.

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^/?([-a-zA-Z0-9_+]+).php$ /$1 [R=301,L]

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

RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?p=$1 [L]
Antony Carthy