0

I need to redirect the request from

www.mysite.com/?querystring=data 

to

www.mysite.com/dir/phpfile.php/?querystring=data 

It means that it should be translated only the url with empty request_uri ( for example

www.mysite.com/css/style.css 

should not be translated), and with not empty query string ( for example the main page

www.mysite.com/ 

should not be translated).

I wrote this code

RewriteCond %{HTTP_HOST} ^www.mysite.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$ 
RewriteRule ^/?(.*)$ www.mysite.com/$1 [QSA]

But it doesn't work. Any suggestion?

2 Answers2

0

The rule that you have has the domain name in it without a protocol, it's going to look like a URI or file pathname to mod_rewrite. Additionally, you're rewriting it nothing, since $1 backreferences the grouping (.*) in your match, which must be nothing since your condition says the URI can only be /. You probably want something like:

RewriteCond %{HTTP_HOST} ^www.mysite.com$ [NC]
RewriteRule ^/?$ /dir/phpfile.php/ [L,R]

The query string will automatically get appended to the end. Remove the R if you don't want to externally redirect the browser (thus changing the URL in the location bar), or replace it with R=301 if you want a permanent redirect.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • I was lazy in defining my needs. Your solution translate also www.mysite.com to www.mysite.com/dir/phpfile.php/. This is not accettable for my site. I edited the question. – Antonio Giovanni Schiavone Jun 25 '13 at 10:34
0
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^(.+)$ [NC]
RewriteRule ^$ /dir/phpfile.php/ [L,QSA,R=302]

Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

anubhava
  • 761,203
  • 64
  • 569
  • 643