1

第一次来这里,我有点像 mod_rewrite 和 regex 菜鸟,所以如果我错过了什么,请告诉我。我整天在这里和其他地方搜索,但没有找到我需要的东西。

我正在寻找一组 RewriteConds 和 RewriteRules 来完成以下任务:

  • 删除 www(如果存在于 URL 中)
  • 重定向到一个文件夹,如果 URL 中还没有
  • 保留 URL 的其余部分

我在一个特定的子文件夹(我们称之为 /webapp)中安装了一个 Web 应用程序,它被配置为在 url 上不需要www。如果用户包含 www,它会向用户显示一个愚蠢的恼人消息。我可以深入研究应用程序并对其重新编程,但我想通过 .htaccess 和 mod_rewrite 为用户处理它,并同时将它们转储到文件夹中,如果他们忘记键入它,则使用 301 重定向完成所有这些操作.

例如,我想要以下任何请求

http://www.mydomain.org/webapp/anything
http://www.mydomain.org/anything
http://mydomain.org/anything

被重定向到

http://mydomain.org/webapp/anything

显然,如果请求一个“正确”的 URL(以 开头http://mydomain.org/webapp/),它根本不会被重写。

到目前为止,我最好的猜测如下:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.mydomain\.org$ [NC]
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=302]

RewriteCond %{REQUEST_URI} !^/webapp.*$ [NC]
RewriteRule ^(.*)$ http://mydomain.org/webapp/$1 [R=302]

这似乎根据http://htaccess.madewithlove.be/工作,但在实践中,并没有那么多。

提前致谢。

4

2 回答 2

0

尝试:

RewriteCond %{HTTP_HOST} ^www\.mydomain\.org$
RewriteRule ^ mydomain.org/$1 [L,R=301]

RewriteCond %{REQUEST_URI} !^/webapp.*$
RewriteRule ^/(.*) mydomain.org/webapp/$1 [L,R=301]
于 2012-10-18T22:38:23.943 回答
0

我自己在这里找到了答案:BowlerHat

看起来像这样:

# First just remove the www
RewriteCond %{HTTP_HOST} ^www\.mydomain\.org$ [NC]
RewriteRule ^(.*)$ http://mydomain.org/$1 [L,R=301]

# Now redirect into the folder
RewriteCond %{REQUEST_URI} !webapp/ [NC]         # if the provided URI does not start with /webapp,
RewriteRule (.*) http://mydomain.org/webapp/ [L,R=301]         # redirect user to /webapp/ root

我决定如果用户尝试访问 mydomain.org/somethingsomething,只是将他们发送到 webapp 的根目录,而不是 /webapp/somethingsomething。

于 2012-10-22T14:27:06.263 回答