Ogugua Belonwu
Ogugua Belonwu

Reputation: 2141

Unable to rewrite "http://www" to "http://" in .htaccess

I have a website and am trying to rewrite all urls with 'http://www...' to 'http://...'

This is the content of my .htaccess

<IfModule mod_rewrite.c>
   Options +FollowSymlinks
    Options -Multiviews
    RewriteEngine on
    RewriteBase /

    RewriteCond %{HTTP_HOST} ^www.example.com$
    RewriteRule ^(.*) http://example.com/$1 [QSA,R=301]

    #RewriteBase /employers/
    RewriteRule ^([a-zA-Z0-9]+)/?$ employers/page.php?page=$1 [L]

    #RewriteBase /candidates/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/(\d{4})/(0?[1-9]|1[0-2])/([^/]+)/?$ candidates/read.php?page=$1&year=$2&month=$3&slug=$4 [L]
</IfModule>

This correctly rewrites these types of urls: www.example.com, www.example.com/index.php

The problem is that it does not rewrite these types: www.example.com/candidates/, www.example.com/candidates/login.php

How can i fix this thanks!

EDIT

I have a .htaccess in my candidates folder and this is the content:

<IfModule mod_rewrite.c>
    RewriteRule ^([^/]+)/(\d{4})/(0?[1-9]|1[0-2])/([^/]+)/?$ read.php?page=$1&year=$2&month=$3&slug=$4 [L]
</IfModule> 

Now if i comment on it, the rewrite rule rewrites properly to 'http://'.

My question: 1. How does the .htaccess affect the 'http://' rewriting? 2. How can i fix it

Thank You

Upvotes: 1

Views: 189

Answers (3)

Samuel Lampa
Samuel Lampa

Reputation: 4392

I actually tested your code above, and it actually works for me!

What I realized though, is that my browser tended to cache the .htaccess settings, and not update even if I changed things, so I'd recommend making a total cleanup of the browser cache and see what happens then. I'm quite sure this helps.

What you could also double-check, is that there are no local .htaccess files under the folders "candidtates" and "employers".

Upvotes: 1

Samuel Lampa
Samuel Lampa

Reputation: 4392

What if you place the two lines:

RewriteCond %{HTTP_HOST} ^www.example.com$`
RewriteRule ^(.*) http://example.com/$1 [QSA,R=301]

... at the end? This kind of change often solved things for me. (I guess the "RewriteCond" can screw things up, if it applies to things below it, which it is not supposed to ...)

Upvotes: 0

Leonid Shevtsov
Leonid Shevtsov

Reputation: 14189

I think you need to add a L (Last) to your first rewrite rule. Like

RewriteRule ^(.*) http://example.com/$1 [QSA,R=301,L]

(which will mean that upon rewriting the domain Apache will stop processing the rest of the rules and will just return a 301 response)

Upvotes: 1

Related Questions