Jump to content

REWRITE help


mattoebs

Recommended Posts

Hi I have 2 different statements I want to rewrite.

 

the first is to rewrite all mysite.co.uk page request to mysite.com, i've done this bit with

 

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.mysite.co.uk$ [NC]

RewriteRule ^(.*)$ http://www.mysite.com/$1 [R,PT]

 

The second statement I need to write is http://www.mysite.com/quote[2-3].php to https://www.mysite.com/quote[2-3].php

 

any help with this will be greatly appreciated.

Link to comment
Share on other sites

Your first part :

 

RewriteCond %{HTTP_HOST} ^www.mysite.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R,PT]

 

Will only rewrite request from www.mysite.co.uk to www.mysite.com. This can be bad if someone type mysite.com or mysite.co.uk.

This will rewrite everything that not www.mysite.com :

 

RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com%{REQUEST_URI} [L,R=301]

 

All request by IP adress, mysite.co.uk, www.mysite.co.uk and mysite.com or any other request that not www.mysite.com will be SEO-friendly redirected to www.mysite.com.

 

If you have many domain in the same root folder you can use this to redirect the www and no-www version to a www.mysite.com version.

RewriteCond %{HTTP_HOST} ^(.*)mysite\.co.uk$ [NC]
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com%{REQUEST_URI} [L,R=301]

 

For the https part you can use :

 

RewriteCond %{HTTPS} off
RewriteRule ^quote(.*)\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Anything that not https AND start with quote AND end with php will be redirected to a https. You can maybe use the full filename instead of wildcards to prevent someone to play with it, though i hardly see how can someone use that to break security, in the end it will always end up in a 301 redirect to your website.

 

To add more safety you can add in your php file something to test if you are on https or not, you can use many variables to make this script domain and file name independent and/or require_once(''); it on each page you need to be secure with https :

<?php

if ($_SERVER['HTTPS'] ....)
{
   /* raise a error or redirect to a https page */
  header('Location: https://www.somesite.com/somepage.php');
}
...
?>

 

The complete code will look like this (untested of course but should work fine) :

Options +FollowSymlinks
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} off
RewriteRule ^quote(.*)\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

The order of the rewritecond/rewriterule is important since you don't want a request like http://mysite.co.uk/quote2.php to be redirect to a https://mysite.co.uk/quote2.php

 

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.