简体   繁体   中英

directing url through htaccess

I know that the code below

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Is use to add www to the HTTP host every time a url is access without www.

similarly the code

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

Is use for posting every thing in the url to index.php in the value of the url variable. WITHOUT CHANGING THE URL LIKE IN THE FIRST EXAMPLE (which change the url from without www to with www).

What I want is little tricky.

I want if there is no www then the first example steps should be performed.

and if there is www, or any other text, for Eg forum.site.com or blog.site.com or www.site.com then like in second example, it should silently direct to a folder of the same name.

eE.g. if it is www, it should rewrite to a folder www/ folowed by the variable.

like a visitor type www.site.com It should load the files from www/ folder and if visitor type www.site.com/index.php?pid=1 then it should be rewritten to www/ folder along with the index.php?pid=1 data

and the same should go for for any thing other than www. like for fourm.site.com/index.php?blah=blahBlah etc.

hope I m not confusing and am not breaking rules.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^.+{3,}
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(.*)\.(.+\..+)
RewriteRule ^(.*)$ http://%2/%1/$1 [QSA,L]

The algorithm of the 2-nd rule is this:

  1. I check, that the host consists of at least 3 parts, separated by dots. It's not an ideal solution, cause there are domains, naturally having 3 parts like .co.uk and .com.br. But, I assume, that you are using a domain of 2 parts.
  2. So, if there are 3 parts, like: www.site.com, I put the 1-st part into %1 and 2-nd part into %2. So, %1 = 'www', %2 = 'site.com'.
  3. Redirect everything to http:://site.com/www/request

If you have 3 parts domain, like site.co.uk, you can make a more complicated rule:

RewriteCond %{HTTP_HOST} ^(.*)\.(.+\..+\..+)
RewriteRule ^(.*)$ http://%2/%1/$1 [QSA,L]

This will rewrite: subdomain.site.co.uk to http://site.co.uk/subdomain/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM