简体   繁体   中英

Redirect *.php to clean URL

My htaccess doesn't quite work:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ /blah/$1 [R=301,L]
RewriteRule ^(.*)$ /blah/index.php/$1 [L]

Basically /blah/about.php should redirect to /blah/about

I'm using codeigniter so the last line is required to process URL's.

The redirect works fine but I get an internal server error in /blah/about

** Edit - error log:

Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

I think you just need to change your last line to be:

RewriteRule ^(.*)$ /blah/index.php?/$1 [L]

I have no specific experience with CodeIgniter but generally frameworks want to push all requests through a central controller (index.php) and then parse the request's URL from within there.

EDIT: Just updated my answer with the example from here: http://codeigniter.com/wiki/mod_rewrite/ , that should be how CodeIgniter wants it.

UPDATE: You should probably duplicate your RewriteConds for the second RewriteRule, they only apply to the first:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ /blah/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /blah/index.php/$1 [L]

Thanks @Cryo for his help, this is the fix:

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

RewriteRule ^(.*)\.php$ /blah/$1 [R=301,L]

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

RewriteRule ^(.*)$ /blah/index.php/$1 [L]

What about:

RewriteRule ^(.*)(\.php)?$ /blah/index.php/$1 [L]

and remove the previous line.

Also make sure that /blah/about.php does not exist.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\$ $1.php [NC]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM