Link to home
Start Free TrialLog in
Avatar of bearclaws75
bearclaws75

asked on

Mod_rewrite: how to redirect to url only when no querystring provided?

All pages on the site are PHP.

I am struggling with several rules in mod_rewrite:

1) rewrite all .php pages as .html extensions
* user enters "myfile.php" and is redirected to "myfile.html"  (this does NOT work)
* user enters "myfile.html" and the page displays properly  (this works)

2) if the URL does not contain a querystring value, redirect the user to a URL with a querystring
* user enters "domain.com" and is redirected to "domain.com/index.html?param=123"

I've added my current code snippet for review.

Thoughts?


<IfModule mod_rewrite.c>
       Options +FollowSymlinks
       RewriteEngine on
       # Rewrite .php files as .html
       RewriteRule ^(.*)\.html$ $1.php [nc]
       # Redirect non-querystring page to index.html with querystring
       RewriteCond %{HTTP_HOST} ^stage.mysite.com/
       RewriteRule ^(.*)$ http://stage.mysite.com/index.html?segment=1 [R=301,L]
   </IfModule>

Open in new window

Avatar of ravenpl
ravenpl
Flag of Poland image

>       # Rewrite .php files as .html
>       RewriteRule ^(.*)\.html$ $1.php [nc]
Actually it rewrites .html -> .php, usually users want just that.

>       # Redirect non-querystring page to index.html with querystring
>       RewriteCond %{HTTP_HOST} ^stage.mysite.com/
>       RewriteRule ^(.*)$ http://stage.mysite.com/index.html?segment=1 [R=301,L]
If You want to rewrite only if no query string, You have to add this check to conditions. But You sure You want to verify HTTP_HOST as well?

       # Redirect non-querystring page to index.html with querystring
       RewriteCond %{HTTP_HOST} =stage.mysite.com
       RewriteCond %{QUERY_STRING} ^$
       RewriteRule .* http://stage.mysite.com/index.html?segment=1 [R=301,L]
Avatar of bearclaws75
bearclaws75

ASKER

No - I don't need the HTTP_HOST, so I removed that.

I tried this:
      # Redirect non-querystring page to index.html with querystring
      RewriteCond %{QUERY_STRING} ^$
      RewriteRule .* http://stage.mysite.com/index.html?segment=1 [R=301,L]

...and the non-querystring page redirects correctly. However, all of the page formatted is removed. When I "view source", the .CSS file show the "index.html" file code as their contents.

My guess is that every page element is being redirected to "index.html".

Is there a way to target *only* "index.php" and/or "index.html"?
ASKER CERTIFIED SOLUTION
Avatar of ravenpl
ravenpl
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ravenpl - your solution works best so far but I have one last condition to address: when the user enters the root domain "http://stage.mysite.com/" - with NO index file - they should be redirected to "index.html?segment=1" as well.

I've included my updated code below.


       # Rewrite .php files as .html
       RewriteRule ^(.*)\.html$ $1.php [nc]

      # Redirect non-querystring and index.html/php only page to index.html with querystring
      RewriteCond %{QUERY_STRING} ^$
      RewriteCond %{REQUEST_URI} \index.(php|html)$
      RewriteRule .* http://stage.mysite.com/index.html?segment=1 [R=301,L]

Open in new window

Try this

      #the RewriteBase is not needed if code used in .htaccess
      RewriteBase /

      #rewrite empty to index
      RewriteRule ^$ index.php
      # Rewrite .html to .php
      RewriteRule ^(.*)\.html$ $1.php [nc]
      # Redirect non-querystring and index.html/php only page to index.html with querystring
      RewriteCond %{QUERY_STRING} ^$
      RewriteCond %{REQUEST_URI} index\.(php|html)$
      RewriteRule .* http://stage.mysite.com/index.html?segment=1 [R=301,L]
I decided to manage the last requirement in PHP so this solution works best. Thanks!