简体   繁体   中英

PHP: htaccess: Rewrite rules are getting 500 ERROR on non existing pages

I have a website that basically hides the .php using RewriteRule and also allow the script to determine the parameter by slashes.

So example: http://www.dealhandler.com/deals/san-jose/Living%20Social/233102

The script is deals.php which takes in parameters like san-jose, living social, and the deal id.

Here's my .htaccess:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^dealhandler.com 
RewriteRule ^(.*)$ http://www.dealhandler.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)(.*)$ $1.php$2 [L,QSA]

PROBLEM: If a visitor enters an invalid url that doesn't have a script on the root level, i get 500 ERROR.

Example: http://www.dealhandler.com/idontexist I'll get:

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.

Is there a way to keep the current function but be smart enough to stop and redirect if the root level file doesn't exist?

At first glance, it appears like you're infinitely adding .php whenever it doesn't find a file with the given name. If you turn on debug logging, you should be able to confirm this easily.

Your best options, are to either 1) Only append .php to strings that don't already end in .php, so it will only happen once or 2) Check if the potential new .php filename exists in another Condition before applying the Rule at all

Try adding this right before your RewriteRule:

RewriteCond %{REQUEST_URI} !^/[^/]+\.php

The requests that don't map to a valid php file (like idontexist.php) should just return a 404.

Here's something that should avoid infinite loops: just test first if it's not a file or a dir, and if ends with "php" then stop immediately. If not the rules carry on and try your own test:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^dealhandler.com 
RewriteRule ^(.*)$ http://www.dealhandler.com/$1 [R=301,L]

# if it's not a file...
RewriteCond %{REQUEST_FILENAME} !-f
# ... and it's not a dir...
RewriteCond %{REQUEST_FILENAME} !-d
# ...and it ends by "php" then stop:
RewriteRule (.*)\.php$ - [L]

# if it's not a file...
RewriteCond %{REQUEST_FILENAME} !-f
# ... and it's not a dir...
RewriteCond %{REQUEST_FILENAME} !-d
# ...rewrite it to php:
RewriteRule ^([^/]*)(.*)$ $1.php$2 [L,QSA]

Check your ../apache/conf/httpd.conf and look for:

#LoadModule rewrite_module modules/mod_rewrite.so

If the hash sign (#) appears as the first letter, remove it, save it and restart server.

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