Reputation: 1134
I have the basic code for redirecting to a 404 custom page if a 404 not found error occurs but this does not always work as expected. I also have a rule to rewrite URLs where page/foo/bar
is redirected to page.php?var1=foo&var2=bar
The rule for this is the following:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^page/(.*)/(.*)$ page.php?var1=$1&var2=$2 [L]
</IfModule>
So, this works very well and I can retrieve the two variables. However, if I write in the URL bar page/a
a error is thrown with the message 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
The message says the error is because of ErrorDocument
rule. If I type a non-existent URL the ErrorDocument
works, but in this particular case it does not.
I also want to mention that if instead of writing page/a
I write page/a/
, the error disappears. So the error is thrown only when I pass one parameter only in the URL and without a /
.
For any other existing page, about
, contacts
etc.., I can easily put or remove the /
and .htaccess
automatically redirects me to the correct page, without the /
This is the code that modifies the URL removing the extension and the /
# ------------------------------------------------------------------------------
# | Remove the slash |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1 [R=301,L]
</IfModule>
# ------------------------------------------------------------------------------
# | Redirect to extensionless url |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=301,L]
</IfModule>
And here is the code for ErrorDocument:
ErrorDocument 404 /boilerplate/template/404.php
Finally, the message in Apache error log:
[core:error] [pid 4400:tid 1672] [client ::1:65310] AH00124: 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.
EDIT
This is the entire part about rewriting
# ##############################################################################
# # URL REWRITES #
# ##############################################################################
# ------------------------------------------------------------------------------
# | Rewrite engine |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /boilerplate
</IfModule>
# ------------------------------------------------------------------------------
# | Remove the slash |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1 [R=301,L]
</IfModule>
# ------------------------------------------------------------------------------
# | Redirect to extensionless url |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=301,L]
</IfModule>
# ------------------------------------------------------------------------------
# | Pretty urls |
# ------------------------------------------------------------------------------
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^about/([^/]+)(?:/([^/]*))?/?$ about.php?var1=$1&var2=$2 [L,NC,QSA]
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
</IfModule>
EDIT 2
So, I deleted everything from .htaccess
except these few rules. Nothing has changed. I cleared the cookies, emptied the cache and cleared all browser history. Also, I tried it with other browsers and the same thing happens. Just to be clear, I don't have another .htaccess
file and the original one contains other snippets from HTML5 Boilerplate, such as caching, compressing and other non re-write related. Now I stripped the file down to these few lines.
Options +FollowSymlinks
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /boilerplate
# ------------------------------------------------------------------------------
# | Redirect to extensionless url |
# ------------------------------------------------------------------------------
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=301,L]
# ------------------------------------------------------------------------------
# | Pretty urls |
# ------------------------------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^page/([^/]+)(?:/([^/]*))?/?$ page.php?var1=$1&var2=$2 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
Upvotes: 0
Views: 570
Reputation: 784878
Replace your page rule with this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]+)(?:/([^/]*))?/?$ page.php?var1=$1&var2=$2 [L,NC,QSA]
Upvotes: 1