.htaccess:添加 www 并直接到子文件夹

发布于 2024-11-06 05:22:04 字数 325 浏览 0 评论 0原文

我正在尝试编写一个 htaccess 脚本,它会自动添加 www 并从子文件夹中提取内容。

这是我用来从子目录中提取的脚本。

RewriteEngine On
RewriteBase /

# pointing for the domain abc.com to folder abc
ReWriteCond %{HTTP_HOST} abc.com
ReWriteCond %{REQUEST_URI} !abc/
ReWriteRule ^(.*)$ abc/$1 [L]  

每当我尝试合并一些代码来自动添加 www 时,尽管它以某种方式搞砸了。

I'm trying to write an htaccess script that both automatically adds the www and pulls the contents from a subfolder.

Here's the script I'm using to pull from a subdirectory.

RewriteEngine On
RewriteBase /

# pointing for the domain abc.com to folder abc
ReWriteCond %{HTTP_HOST} abc.com
ReWriteCond %{REQUEST_URI} !abc/
ReWriteRule ^(.*)$ abc/$1 [L]  

Whenever I try to incorporate some code to automatically add www though it screws up in one way or another.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑色毁心梦 2024-11-13 05:22:04

像这样的东西应该有效。

<IfModule mod_rewrite.c>
#Turn on rewriting
RewriteEngine on
# Rewrite the non www. version
RewriteCond %{HTTP_HOST} !^www.abc.com$
RewriteRule ^(.*)$ http://www.abc.com/abc [L]
# Rewrite the www. version
RewriteCond %{HTTP_HOST} ^www.abc.com$
RewriteRule ^(.*)$ http://www.abc.com/abc [L]
</IfModule>

Something like this should work.

<IfModule mod_rewrite.c>
#Turn on rewriting
RewriteEngine on
# Rewrite the non www. version
RewriteCond %{HTTP_HOST} !^www.abc.com$
RewriteRule ^(.*)$ http://www.abc.com/abc [L]
# Rewrite the www. version
RewriteCond %{HTTP_HOST} ^www.abc.com$
RewriteRule ^(.*)$ http://www.abc.com/abc [L]
</IfModule>
谜兔 2024-11-13 05:22:04

像这样的事情应该有效:

RewriteEngine On
RewriteBase /

# Rewrite to include www
RewriteCond %{HTTP_HOST} ^abc\.com [NC]
RewriteRule ^(.*)$ http://www.abc.com/ [L,R=301]

# Rewrite for the domain abc.com to folder abc
ReWriteCond %{HTTP_HOST} ^www\.abc\.com [NC]
ReWriteCond %{REQUEST_URI} !abc/ [NC]
ReWriteRule ^(.*)$ abc/$1 [L]

我喜欢转义 的任何实例。在 URL 中,因为在正则表达式中,点将匹配任何内容,通常我会随意添加 no case [NC] 标志。第一条规则应重定向以包含 www,第二条规则应将所有其他路径定向到 abc 路径。

Something like this should work:

RewriteEngine On
RewriteBase /

# Rewrite to include www
RewriteCond %{HTTP_HOST} ^abc\.com [NC]
RewriteRule ^(.*)$ http://www.abc.com/ [L,R=301]

# Rewrite for the domain abc.com to folder abc
ReWriteCond %{HTTP_HOST} ^www\.abc\.com [NC]
ReWriteCond %{REQUEST_URI} !abc/ [NC]
ReWriteRule ^(.*)$ abc/$1 [L]

I like to escape any instance of . in a URL because in a regex a dot will match anything and generally I add the no case [NC] flag liberally. The first rule should redirect to include the www and the second rule should direct all other paths to the abc path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文