Solved

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

Posted on 2010-09-15
6
446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 

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

Supports up to 4K resolution!

The VS192 2-Port 4K DisplayPort Splitter is perfect for anyone who needs to send one source of DisplayPort high definition video to two or four DisplayPort displays. The VS192 can split and also expand DisplayPort audio/video signal on two or four DisplayPort monitors.

Question has a verified solution.

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

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
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.:
Suggested Courses

623 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