简体   繁体   中英

mod_rewrite rule 500 internal server error

I want to redirect from extension URL to extensionless one. For example, /contact should read the contents of /contact.php. I use the following rules to achieve this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

There is a file called test.php that has a variable called "id". I want to redirect read requests from /test/1 to test.php?id=1

I use the following to achieve this:

RewriteRule ^test/([0-9]+)/?$            /test?id=$1      

For some reason calling the test/1 file leads to a 500 internal server error. If I remove the rules to hide the php extension, it works again

Also how can I force a permanent redirect on the URL with extension? For example if someone ties to reach /contact.php should be redirected to /contact

Thanks

You can use this code:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# your existing rule (fixed)
RewriteRule ^test/([0-9]+)/?$ /test.php?id=$1 [L,NC,QSA]

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]

## To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L]

This is in my htaccess ... Be sure to have mod_rewrite turned on in Apache...

RewriteEngine On
# Redirect .php requests to url without an extension
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
RewriteRule ^([^/.]+)$ $1.php [L]
# This will remove the trailing slash unless it's a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

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