Reputation: 536
hello I have a website with a main domain , and subdomains.. running over apache web server and centOS... I also have a url rewrite mechanism such as
http://subdomain.domain.com/buy-a-new-car
Id like to redirect all subdomain requests to main domain, keeping the url rewrite like this:
http://domain.com/buy-a-new-car
the .htaccess code i have so far results in this :
http://domain.com/index.php?buy-a-new-car
id like to get rid of the ( index.php? ) part, but I am new to writing .htaccess directives and confused by REGEX
here is my current code :
RewriteEngine on
RewriteBase /
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
any help would be greatly appreciated !
Upvotes: 1
Views: 2131
Reputation: 7729
Why not use Redirect
and/or RedirectMatch
and save the Rewrite stuff for the real rewriting of URLs? See http://httpd.apache.org/docs/current/rewrite/avoid.html
I find this a much more "self documenting" approach which doesn't force you to understand all the rewrite logic each time you go in there.
Upvotes: 0
Reputation: 143886
You need your redirects to happen first, then at the end, do your routing to index.php
:
RewriteEngine on
RewriteBase /
Options -Indexes -Multiviews
# redirect subdomains to main domain
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
# redirect direct accesses to index.php
RewriteCond %{THE_REQUEST} \ /index\.php\?/([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /%1?%2 [L,R=301]
# route everything to index.php internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1
Upvotes: 2