Forum Moderators: phranque
[1]http://example.com/foo.php [2]http://example.com/foo
RewriteEngine on
AddType application/x-httpd-php .xml
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^blog\/?(.*)$ "http\:\/\/blog\.example\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^movies\/?(.*)$ "http\:\/\/movies\.example\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^sms\/?(.*)$ "http\:\/\/sms\.example\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^articles\/?(.*)$ "http\:\/\/articles\.example\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^admin\/?(.*)$ "http\:\/\/admin\.example\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^resources\/?(.*)$ "http\:\/\/resources\.example\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^images\/?(.*)$ "http\:\/\/images\.example\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^js\/?(.*)$ "http\:\/\/js\.example\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^css\/?(.*)$ "http\:\/\/css\.example\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^feeds\/?(.*)$ "http\:\/\/feeds\.example\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^newsletter\/?(.*)$ "http\:\/\/newsletter\.example\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ "http://example.com/$1" [R=301,L]
RewriteRule %{REQUEST_URI} !\.(gif|jpe?g|png|css|js|zip)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
[edited by: jdMorgan at 1:39 pm (utc) on Jun 19, 2010]
[edit reason] Pleasu use example.com only. [/edit]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$ RewriteCond %{HTTP_HOST} ^(www\.)?example\.com RewriteRule ^blog\/?(.*)$ "http\:\/\/blog\.example\.com\/$1" [R=301,L] RewriteRule ^blog(/(.*))?$ http[i][/i]://blog.example.com/$2 [R=301,L] [edited by: g1smd at 12:22 am (utc) on Jun 19, 2010]
hmm all that coding is done by cpanel.. i didnt do it.. so if i change it, cpanel wont recognise code. And maybe it will screw uo with stats, info, any any other thing that may rely on it..
you really think it's a good idea coz i dont want to mess things up.. now at-least i can control the subdomains from the cpanel.. if i change the code, they wont show up there and you see i am not that good with .htaccess after all.. php is cool but htaccess codes just dont get into my head.. plus exams coming, alot of homework.. i really cnnot afford reading documentation.. but you guys have helped me alot.. now i can atleast copy-paste small chunks of code to make it work as i want to.. :)
Personally, I want to beat the person who programmed cPanel to a pulp. It produces some of the worst, malformed, inefficient, and just blatantly wrong, .htaccess code on the planet.
Your third rule will cause a multi-step redirection chain. It needs to be moved to be the 14th rule (just before the rewrite at the 15th) to stop that happening.
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ "http://example.com/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.([^.:]+\.)?example\.com
RewriteRule ^(.*)$ http://%1example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index$ http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index(\.php)?([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.php)?$ http://example.com/$1 [R=301,L]
RewriteEngine on
AddType application/x-httpd-php .xml
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index(\.php)?([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.php)?$ http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.([^.:]+\.)?example\.com
RewriteRule ^(.*)$ http://%1example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^blog(/(.*))?$ http://blog.example.com/$2 [R=301,L]
#similar code for other subdoamins goes here..
RewriteRule %{REQUEST_URI} !\.(gif|jpe?g|png|css|js|zip)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
if you want to have 'complex' redirects and rewrites on your site, it is time to stop using control panel
The better solution was given by g1smd above -- Use a negative-match RewriteCond on specific rules to prevent them form being applied to /blog requests
Instead of replacing the "/index.php" redirect with your "/index" redirect, I suggest that you do both. Either use separate rules, or make the ".php" part optional in both patterns.
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index(\.php)?([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.php)?$ http://example.com/$1 [R=301,L]
Follow the external redirects with all internal rewrites
You have placed the "remove www" redirect *after* the internal rewrite that is used to map subdomains to subdirectories.
domain canonicalization redirect rules are almost always the last redirect rules in a properly-coded file
AddType application/x-httpd-php .xml
#
RewriteEngine on
#
# Redirect direct client requests for /<any-directory>/index.php
# or /<any-directory>/index to /<any-directory>/
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index(\.php)?([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.php)?$ http://%{HTTP_HOST}/$1 [R=301,L]
#
# Redirect direct client requests for /<any-directory>/<any-page>.php
# to /<any-directory>/<any-page> to 'force' extensionless URLs
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://%{HTTP_HOST}/$1 [R=301,L]
#
# Redirect requests for all subdomain-subdirectories to subdomains
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^([b]blog|movies|sms|admin|resources|images|js|css|feeds|newsletter[/b])(/(.*))?$ http://$1.example.com/$3 [R=301,L]
#
# Remove "www" and redirect to canonical hostnames
RewriteCond %{HTTP_HOST} ^www\.([^.:]+\.)?example\.com
RewriteRule ^(.*)$ http://%1example.com/$1 [R=301,L]
#
#
# Rewrite all other non-media requests to script if script exists
RewriteRule %{REQUEST_URI} !\.(gif|jpe?g|png|css|js|zip)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://%{HTTP_HOST}/$1 [R=301,L]
AND
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index(\.php)?([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.php)?$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/blog/ would maybe look for something like example.com/blog and not blog.example.com
so unless and until someone manually changes the URL
# Redirect rewritten client requests for /<subdomain-subdirectory>/<any-directory>/<any-page>.php to
# <subdomain>.example.com/<any-directory>/<any-page> to 'force' extensionless URLs, excluding "/blog/"
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(movies|sms|admin|resources|images|js|css|feeds|newsletter)/(([^/]+/)*[^.]+)\.php http://$1.example.com/$2 [R=301,L]
#
# Redirect direct client requests for /<any-directory>/<any-page>.php to
# /<any-directory>/<any-page> to 'force' extensionless URLs, excluding "/blog/"
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteCond %{HTTP_HOST} ^(((movies|sms|admin|resources|images|js|css|feeds|newsletter)\.)?example\.com)
RewriteRule ^(([^/]+/)*[^.]+)\.php http://%1/$1 [R=301,L]
Make sure you register both the www and non-www URLs for every subdomain of your site in Google WebmasterTools. Look very carefully at all of the reports.
I also want to re-emphasize that doing it this way instead of improving the hosting set-up is not a good long-term solution.
$array = explode('.', $_SERVER['HTTP_HOST']);
if($array[0]=='www') {header('HTTP/1.1 301 Moved Permanently'); header('Location: http://'.$_SERVER['SERVER_NAME'].strip_tags($_SERVER['REQUEST_URI']));}