Ogugua Belonwu
Ogugua Belonwu

Reputation: 2141

Resolving conflicting url rewrite rules

I am working on a website and i am faced with a big issue is:

  1. i want all my traffic http://www.example.com/ to http://example.com or http://www.example.com/folder/ to http://example.com/folder/

(Note: the key thing here is it can work for infinite number of subfolders or pages as far as its the same root domain)

  1. I want to rewrite http://example.com/name to http://example.com/folder/page.php?page=$name except for some urls like http://example.com/css which i will specify. This time around, the url still shows http://example.com/name which is the page visited but really page has been rewritten. This is unlike the first scenario that that changes the url in the address bar.

Now this is what i have:

<IfModule mod_rewrite.c>
   Options +FollowSymlinks
    Options -Multiviews
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

   RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
   RewriteCond %{REQUEST_URI} !^/(candidates|css|employers|layout|pics|securimage)/?$

   RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
   RewriteRule ^([^/]+)/?$ employers/page.php?page=$1 [L]
</IfModule>

The issues am having with what i have above are:

  1. It does not redirect http://www.example.com/ to http://example.com.

  2. It seems to rewrite well for http://www.example.com/name but when i have a url with one of the exceptions like http://example.com/css it changes the url to http://example.com/css/?page=css.

Please how can i fix this.

Thank you so much.

Upvotes: 1

Views: 411

Answers (1)

gregseth
gregseth

Reputation: 13408

You have to separate your conditions ans rewrite rules:

For the www subdomain:

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

then for the QSA redirection, except if the file exists:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ employers/page.php?page=$1 [L]

Upvotes: 0

Related Questions