Solved

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

Posted on 2010-09-15
6
442 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
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 several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

895 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

14 Experts available now in Live!

Get 1:1 Help Now