强制www和斜线与htaccess的的mod_rewrite在多域站点 [英] Enforce www and trailing slash with mod_rewrite htaccess on a multi-domain site

查看:89
本文介绍了强制www和斜线与htaccess的的mod_rewrite在多域站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是加强WWW和网址斜线来实现这一目标,在多域环境:

http://mydomain.com/company -> http://www.mydomain.com/company/

美中不足的是,某些子域必须被排除在WWW preFIX,但仍然执行最后的斜线,如:

The catch is that certain sub domains must be excluded from the www prefix, yet still enforce the trailing slash, such as:

http://m.mydomain.com/company -> http://m.mydomain.com/company/

另外的因素是,我需要保持所有子域名,以避免多重重定向。例如:

The additional factor is that I need to maintain any sub domain to avoid multiple redirects. For example:

http://dev.mydomain.com/login -> http://dev.mydomain.com/login/

多重定向将导致:

Multiple redirects would result in:

http://dev.mydomain.com/login -> http://mydomain.com/login/

这需要避免,以保持用户对当前开发或移动网站。

This needs to be avoided to keep the user on the current dev or mobile site.

我到目前为止有:

# Convert http://mydomain.com/company -> http://www.mydomain.com/company/
# Enforce www and trailing slash
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|dev|m)\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]

这适用于已丢失了www preFIX和斜线和1重定向增加了他们两个域。

This works for a domain that is missing the www prefix and trailing slash and adds them both in 1 redirect.

# Convert http://mydomain.com/company/ -> http://www.mydomain.com/company/
# Enforce www
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|dev|m)\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

这工作,如果斜线为present,并增加了WWW preFIX如果它丢失了。

This works if a trailing slash is present and adds the www prefix if it is missing.

我无法弄清楚是重定向用户,如果一个子域名为present和结尾的斜线缺少的方法。

RewriteCond %{QUERY_STRING} ^$    [NC]
RewriteRule ^((.*/)?[^/\.]+)$ /$1/ [R=301,L]

此规则添加斜线,但它会重定向一个子域回到根域。

This rule works to add a trailing slash, but it will redirect a sub domain back to the root domain.

您能推荐相结合的这些规则到尽可能少的选项,可能的解决方案,但实现了我的目标,避免了多个重定向?

推荐答案

对于 重写规则 的指令,则模式匹配的对应于该请求的文件系统路径。因此,无论使用 的RewriteCond 用合适的变量,或建立更换时,生成的URL,而不是一个路径中使用适当的变量。

For the RewriteRule directive, the pattern is matched against the file system path corresponding to the request. So either use a RewriteCond with a suitable variable, or use proper variables when building the replacement, to generate a URL instead of a path.

您是否尝试过

RewriteCond %{QUERY_STRING} ^$    [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^((.*/)?[^/\.]+)$ http%1://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]

这应该是同步的配方最与其他两个规则。你将不得不增加一个条件,虽然。

That should be the formulation most in sync with your other two rules. You will have to add a condition to that, though.

这篇关于强制www和斜线与htaccess的的mod_rewrite在多域站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆