开发者

.htaccess: Rewrite only if there is no subdirectory

开发者 https://www.devze.com 2023-02-01 13:35 出处:网络
With this rules: RewriteCond %{HTTP_HOST} !^www\\.domain\\.com RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]

With this rules:

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

You can make every URL in the si开发者_StackOverflow社区te redirect to the URL plus the subdomain www. at the beginning.

The problem I am having is that it is giving me some problems with other subdomains, i.e. I wouldn't want blog.domain.com to me redirected too.

So is there any way to do a generic rule to avoid the redirect of anything that already has a subdomain?

Thanks in advance!!


I hope the question is clear enough!! I you need any clarification please ask for it!


yes - your regex for the RewriteCond matches on "anything that is not www.domain.com". If I understand correctly, you want to match only domain.com.

Like this:

RewriteCond %{HTTP_HOST} ^domain\.com$ 
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]

now if someone uses domain.com, they'll be redirected. For requests that have anything other than domain.com, they will not be redirected. This includes www.domain.com as well as blog.domain.com, as well as dns2.domain.com.


Thank you haha for leading me in the right direction.

I managed to do it through cPanel, I redirected all non www. with a www. subdomain with wildcards.

Sorry, didn't know it was possible to do specifically this.

Thank you for all the answers and comments!!

0

精彩评论

暂无评论...
验证码 换一张
取 消