Solved

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

Posted on 2010-09-15
6
444 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux script delete files 3 51
CENTOS DHCP Server / PXE/TFTP 14 157
Connect to CentOS (in a local VMWare VM) using Putty 7 69
Linux MD5 Hash 7 42
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 …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

821 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