Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache mod_rewrite:删除www并重定向到文件夹(如果URL中还没有)_Apache_.htaccess_Mod Rewrite_Redirect - Fatal编程技术网

Apache mod_rewrite:删除www并重定向到文件夹(如果URL中还没有)

Apache mod_rewrite:删除www并重定向到文件夹(如果URL中还没有),apache,.htaccess,mod-rewrite,redirect,Apache,.htaccess,Mod Rewrite,Redirect,第一次来这里,我是一个有点mod_重写和regex noob的人,所以如果我遗漏了什么,请告诉我。我在这里和其他地方搜索了一整天,但没有找到我真正需要的东西 我正在寻找一组重写条件和重写规则,它们将完成以下任务: 删除URL中的www(如果存在) 重定向到某个文件夹(如果URL中尚未存在) 保留URL的其余部分 我在一个特定的子文件夹(我们称之为/webapp)中安装了一个web应用程序,该子文件夹配置为需要在url上无www。如果用户包含www,它会给用户带来一条愚蠢而烦人的消息。我可以深

第一次来这里,我是一个有点mod_重写和regex noob的人,所以如果我遗漏了什么,请告诉我。我在这里和其他地方搜索了一整天,但没有找到我真正需要的东西

我正在寻找一组重写条件和重写规则,它们将完成以下任务:

  • 删除URL中的www(如果存在)
  • 重定向到某个文件夹(如果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]
这似乎是按照计划进行的,但在实践中并没有这么多

提前感谢。

试试:

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]

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

看起来像这样:

# 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。

不可以。去掉“http://”部分意味着“www.mydomain.org”重定向到“www.mydomain.org/home/accountname/webapp/”“mydomain.org”显示index.html文件,“mydomain.org/ErrorPath”给出错误路径的404。