Tutorial :mod_rewrite “too many redirects” problem



Question:

Trying,

  <IfModule mod_rewrite.c>    RewriteEngine On    RewriteCond %{REQUEST_METHOD} ^TRACE    RewriteRule .* - [F]    RewriteCond %{HTTP_HOST} ^(.*)dev\.example\.edu$ [NC]    RewriteRule ^/test(.*)$ http://dev.example.edu/test/index.php/test$1 [NC]    </IfModule>  

on an apache 2.2 server, to get this rewrite working to hide the "index.php/test" part of the path.

everything i've tried either loops the url part (index.php/test) within the address bar or gives a "too many redirects" error.

i suspect that the "test" part of the equation being on both sides is throwing it off but am not sure how to get it to work.

i just want: dev.example.edu/test/index.php/test* to rewrite to: dev.example.edu/test/*

thanks


Solution:1

You need to exclude the destination path to avoid an infinite recursion:

RewriteCond %{HTTP_HOST} ^(.*)dev\.example\.com$ [NC]  RewriteCond $1 !^/index\.php/test/  RewriteRule ^/test/(.*)$ http://dev.example.com/test/index.php/test/$1 [NC]  

Here the match of the first grouping ($1) is checked not to match ^/index\.php/test/.

But if you want to rewrite /test/index.php/test/… to /test/…, you will rather need this rule:

RewriteCond %{HTTP_HOST} ^(.*)dev\.example\.com$ [NC]  RewriteRule ^/index\.php/test/(.*)$ http://dev.example.com/test/$1 [NC]  


Solution:2

Per Jim at webmasterworld (thanks!)

"The [P] flag invokes a reverse-proxy request to the server at the designated URL; That is, it opens a new out-going HTTP connection and sends a request to that server. So at the very least, your configuration is twice as slow as it should be, just using the original working rule, because your server is sending itself a new request via HTTP instead of just serving the content from a non-default-mapped filepath.

It seems to me that all that's needed is an internal rewrite, so that requests for the resource at URL http://dev.example.edu/distribution/ are served with the content generated by the script at the server filepath /distribution/index.php/distribution/"

RewriteEngine on   #   # Return 403-Forbidden response to TRACE requests   RewriteCond %{REQUEST_METHOD} ^TRACE   RewriteRule .* - [F]   #   # Internally rewrite requests for URL-path /recreation/<anything>   # to filepath /eel/index.php/recreation/<anything>   RewriteCond %{HTTP_HOST} ^dev\.example\.edu [NC]   RewriteRule ^/recreation/(.*)$ /ee1/index.php/recreation/$1 [L]   #   # Internally rewrite requests for URL-path /distribution/<anything>   # to filepath /distribution/index.php/distribution/<anything>   RewriteCond %{HTTP_HOST} ^dev\.example\.edu [NC]   RewriteRule ^/distribution/(.*)$ /distribution/index.php/distribution/$1 [L]  

So I think I was just making it more complicated than it had to be. I've removed the P flag and removed the full server address from the rewriterule.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »