简体   繁体   中英

Adding mod_rewrite rule causes server error for whole directory

I have a test folder (example.com/test) with it's own htaccess file. It looks like this:

RewriteEngine On

RewriteRule ^(projects|about)/?$ /test/$1.php [L]
RewriteRule ^sitemap\.xml$ /test/xmlsitemap.php [L]

RewriteRule ^([^/]+)/?$ /test/projects.php?project=$1 [L] #this line causes trouble
RewriteRule ^([^/]+)/([0-9]+)/?$ /test/posts.php?&project=$1&post=$2 [L]
RewriteRule ^([^/]+)/([0-9]+)/([^/]+)/?$ /test/posts.php?project=$1&post=$2&title=$3 [L]

When I add this line: RewriteRule ^([^/]+)/?$ /test/projects.php?project=$1 [L] to my .htaccess file, everything in the test directory give a 500 Internal Server Error . All the other rules work like they should.

What I'm trying to do is make example.com/test/$1 go to the example.com/test/projects.php?project=$1 , with the exceptions of "projects," "about," and "sitemap.xml." There are also numbered posts in each project that have an optional title portion of the url.

The server is running apache 2.2.15.

Please let me know if you need more info or want me to test something.

Try this .htaccess in /test/ folder:

RewriteEngine On
RewriteBase /test/

RewriteRule ^(projects|about)/?$ $1.php [L]
RewriteRule ^sitemap\.xml$ xmlsitemap.php [L]

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/?$ projects.php?project=$1 [L,QSA]
RewriteRule ^([^/]+)/([0-9]+)/?$ posts.php?&project=$1&post=$2 [L,QSA]
RewriteRule ^([^/]+)/([0-9]+)/([^/]+)/?$ posts.php?project=$1&post=$2&title=$3 [L,QSA]

The rule you have in place now should result in the following:

http://example.com/test/1 -> http://example.com/test/posts.php?&project=test&post=1 

Assuming you are not seeing anything useful in access or error logs, then consider adding a rewrite log entry to the host.

RewriteLog /var/log/httpd/rewrite.log
RewriteLogLevel 9

This could help to detect something like a redirect loop which could be the cause of the internal server error.

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