Solved

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

Posted on 2010-09-15
6
440 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:bearclaws75
  • 3
  • 3
6 Comments
 
LVL 43

Expert Comment

by:ravenpl
ID: 33689257
>       # 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]
0
 

Author Comment

by:bearclaws75
ID: 33692149
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"?
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 500 total points
ID: 33692385
True, we should
      # Redirect non-querystring-and-html-php-only page to index.html with querystring
      RewriteCond %{QUERY_STRING} ^$
      RewriteCond %{REQUEST_URI} \.(php|html)$
      RewriteRule .* http://stage.mysite.com/index.html?segment=1 [R=301,L]
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:bearclaws75
ID: 33712035
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

0
 
LVL 43

Expert Comment

by:ravenpl
ID: 33712154
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]
0
 

Author Closing Comment

by:bearclaws75
ID: 33712417
I decided to manage the last requirement in PHP so this solution works best. Thanks!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now