apache .htaccess 在没有 www 的情况下重写并重定向到子目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/782143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.htaccess rewrite without www AND redirect to subdirectory
提问by
I'd like to redirect
我想重定向
www.example.com/*toexample.com/*
www.example.com/*到example.com/*
And at the same timeredirect
而在同一时间的重定向
example.com/*toexample.com/forum/*
example.com/*到example.com/forum/*
But I also have /wiki/and /blog/and /style/, so I don't wantto redirect
但我也有/wiki/和/blog/和/style/,所以我不想重定向
example.com/style/*toexample.com/forum/style/*
example.com/style/*到example.com/forum/style/*
This is what I have at the moment, which is not working quite correctly:
这就是我目前所拥有的,它无法正常工作:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/forum/
RewriteRule ^(.*)$ forum/ [R=301,L]
Clarification: my question can be asked in a simpler way.
澄清:我的问题可以用更简单的方式提出。
I'd like to redirect an empty REQUEST_URIor /, or a non-existent file only if it is in the root directoryto /forum/.
我想将一个空的 REQUEST_URI或/或一个不存在的文件重定向到仅当它在根目录中时/forum/。
回答by inxilpro
Try this:
尝试这个:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://example.com/ [R=301,QSA,L]
RewriteCond %{REQUEST_URI} !^/(wiki|blog|style|forum)
RewriteRule ^(.*)$ http://www.example.com/forum/ [R=301,QSA,L]
回答by Gumbo
I would use these rules:
我会使用这些规则:
# redirect www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^ http://example.com%{REQUEST_URI} [L,R=301]
# prefix everything but /forum/, /wiki/, /blog/, /style/ with /forum/ and rediret to it
RewriteRule !^(forum|wiki|blog|style)/ /forum%{REQUEST_URI} [L,R=301]
The second rule could additionally be replaced by this one to check the existence of the first path segment for every request.
第二条规则可以另外替换为这条规则,以检查每个请求的第一个路径段是否存在。
# check if first segment of requested URI path is either missing
RewriteCond # Enforce www
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
RewriteRule ^(.*)$ /exemple/ [L,R=301]
# Enforce NO www
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^(.*)$ http://exemple.com/ [L,R=301]
^$ [OR]
# or cannot be mapped to an existing directory
RewriteCond %{DOCUMENT_ROOT}RewriteEngine on
RewriteRule ^forum/(.*)$ forum/ [L]
RewriteRule ^wiki/(.*)$ wiki/ [L]
RewriteRule ^blog/(.*)$ blog/ [L]
RewriteRule ^style/(.*)$ style/ [L]
RewriteRule ^(.*)$ forum/ [L]
RewriteCond %{HTTP_HOST} ^www.example\.com$
RewriteRule ^(.*)$ http://example.com/
/ !-d
RewriteRule ^[^/]* /forum%{REQUEST_URI} [L,R=301]
回答by Gabtheviking
I don't have the answer for everything but for your www/no wwwproblem you could try this :
我没有所有问题的答案,但对于您的www/no www问题,您可以尝试以下操作:
##代码##回答by Tommi Forsstr?m
I'd say this should work.
我会说这应该有效。
##代码##
