How to stop .htaccess loop
I am not expert in htaccess files and I am trying something that seems very simple but I have not been able to do it.
I searched and finally find something here could work for my use but it didn't. The code is below.
Here is an example of what i need:
http://localhost/Test/TestScript.php
to show this: http://localhost/Test/TestScript/
with script in original place.
These are my rules (Copied):
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /Test
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s([^.]+).php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]
This rule give:
Forbidden You don't have permission to access ".../Test/TestScript.php on this server."
So changed last line to:
RewriteRule ^.* http://localhost/Test/TestScript.php [L]
But now get this error:
The page isn't redirecting properly
Hope brilliant people in this place can help me. Thank you.
The rule-set ends up in a loop. Let's see:
The request is http://localhost/Test/TestScript.php
is redirected to http://localhost/Test/TestScript/
, for the browser to show it, and finally is trying to be mapped back to the original resource.
The [L] flag in the rule doesn't stop the process, as many people think. The rewriting engine loops through the complete rule-set, rule by rule, and when a particular rule matches, it loops through the corresponding conditions, if any. As this process is repeated for each request and rules generate new requests, it is easy to enter an infinite loop.
That's what the message "The page isn't redirecting properly" means in this case.
Here are The Technical Details of this process
Some solutions:
I) The best and more practical one is to use directly the "pretty" URL in the initial request, mapping it silently to the resource. This is a one step process where the "pretty" URL is always displayed in the browser's address bar. One of the advantages of this option, is that nothing in the URI-path of the incoming URL has to exist .
http://localhost/Test/TestScript/
http://localhost/Test/TestScript.php
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# Prevent loops
RewriteCond %{REQUEST_URI} !.php [NC]
# Map internally to the resource, showing the "pretty" URL in the address bar
RewriteRule ^([^/]+)/([^/]+)/? /$1/$2.php [L,NC]
II) If that's not possible because there are already links pointing directly to the resource, one way to show the "pretty" URL but still get the data from the original request, is to make a visible and permanent redirect first, stripping the extension to display the "pretty" URL, and then an internal rewrite back to the original resource.
http://localhost/Test/TestScript.php
, http://localhost/Test/TestScript/
the "pretty" URL, http://localhost/Test/TestScript.php
, the original request. Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# Get the URI-path directly from THE_REQUEST variable
RewriteCond %{THE_REQUEST} ^(GET|HEAD)s/([^/]+)/([^.]+).php [NC]
# Strip the extension and redirect permanently
RewriteRule .* /%2/%3/ [R=301,L,NC]
# Now the browser bar shows `http://localhost/Test/TestScript/`
# Prevent loops
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.php [NC]
# Map internally to the original resource
RewriteRule ^([^/]+)/([^/]+)/? /$1/$2.php [L,NC]
NOTES:
mod_rewrite
is enabled. Most likely the loop is due to a quirk in .htaccess processing, not anything you've coded. (The quirk is so bad that a simple .htaccess with out a loop is more surprising than one with:-) If it loops, don't just immediately assume you've got some sort of logic error or have miscoded something.
In .htaccess rules (unlike the same rules in httpd.conf) the [L]ast flag starts all over at the top. (See the loop possibilities being enabled?-)
Some typical solutions (in addition to the ones listed above) are:
Option 1: Use the [END] flag rather than the [L]ast flag on lines where you really do intend a complete and immediate exit from the .htaccess file in that subdir. (Problem is, the [END] flag is only available in newer [version 2.3.9 and later] Apaches, and won't even "fall back" in earlier versions.)
Option 2: Add boilerplate code like this at the top of each of your .htaccess files:
RewriteCond %{ENV:REDIRECT_STATUS} !^[s/]*$
RewriteRule ^ - [L]
I'm no expert with .htaccess but don't you need to redirect at some point?
Such as: redirect http://www.yoursite.com/test/testscript/
Again I'm no expert. But maybe try checking out this: http://forums.techguy.org/web-design-development/717997-solved-htaccess-redirect-loop-problem.html
链接地址: http://www.djcxy.com/p/67222.html上一篇: 在Javascript中将数组推入数组(jQuery)
下一篇: 如何停止.htaccess循环