Tuesday, May 15, 2018

apache 2.2 - Redirect problem using .htaccess mod_rewrite only for root



I'm trying to permanently redirect all requests to my root directory to another site. I don't want anything other than the root requests to be redirected.



Requests to "http://www.example.com" should to to "http://www.example2.com/blah"




I can get this working with the following:



RewriteEngine On
Rewriterule ^/$ http://www.example2.com/blah [L,R=301]



Everything seems to work fine (all requests other than root remain not redirected). Except that one particular type of request doesn't work.



I have a PHP script that runs at "http://www.example.com/phpscript"




Requests to that script have an extra component in the url like "http://www.example.com/phpscript/blah"



I strip out the /blah part within the PHP script and return an gif image based on the request. This may be the source of my problem. Requests to this url don't work with my above rewriterule.



Any ideas? Thanks in advance.


Answer



You possibly want



Rewriterule ^$ http://www.example2.com/blah [L,R=301]



(No slash)



If this doesn't work, you should try adding this to your http config (Somewhere in the config, it doesn't work at htaccess or virtualhost level)



RewriteLog /tmp/rewrite.log
RewriteLogLevel 9


This will provide you with a line by line explaination of what it's trying to match against what regex, and what the final decision is.




Remove these lines afterwards, otherwise one day you'll discover you don't have nearly as much disk space as you thought you should...


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...