从mod_rewrite的URL删除两个子目录 [英] Remove two subdirectories from url with mod_rewrite

查看:237
本文介绍了从mod_rewrite的URL删除两个子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图摆脱对我的网站的一些子目录。目前,当我需要访问我用这个 username.mywebsite.com/public/user/settings.php 用户设置,我希望它看起来像 username.mywebsite.com/settings.php 。在我的根目录下有如下的htaccess文件:

的RewriteCond%{REQUEST_URI}!^ /公/用户

重写规则^ /(。*)$ /公/用户/ $ 1

和它不工作。此外,我必须修改HTML HREF这样< A HREF =/ settings.php文件> ?谢谢


解决方案

您需要在您的正则表达式来摆脱主导 / 。当URI是通过规则在htaccess文件发送的斜线被剥离。此外,您应该检查,看看是否目的地改写成之前存在/公/用户/ 目录:

 的RewriteCond%{REQUEST_URI}!^ /公/用户
的RewriteCond%{DOCUMENT_ROOT} /公/用户%{REQUEST_URI} -f [OR]
的RewriteCond%{DOCUMENT_ROOT} /公/用户%{REQUEST_URI} -d
重写规则^(。*)$ 1 /公/用户/ $ [L]


  

另外,我必须修改HTML HREF这样< A HREF =/ settings.php文件>


是,该规则只适用于一个方向,正则表达式的的目标。决不周围的其他方法。所以,你需要确保你的内容有,你要使用的URL。

I'm trying to get rid of some subdirectories on my website. Currently when I need to access user settings I use this username.mywebsite.com/public/user/settings.php and I want it to look like username.mywebsite.com/settings.php. In my root directory a have a htaccess file with the following:

RewriteCond %{REQUEST_URI} !^/public/user

RewriteRule ^/(.*)$ /public/user/$1

and it's not working. Also, do I have to modify the html href like this <a href="/settings.php">? Thank you

解决方案

You need to get rid of the leading / in your regex pattern. The slash is stripped off when URI is sent through rules in an htaccess file. Also, you should instead check to see if the destination exists before rewriting into the /public/user/ directory:

RewriteCond %{REQUEST_URI} !^/public/user
RewriteCond %{DOCUMENT_ROOT}/public/user%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/public/user%{REQUEST_URI} -d
RewriteRule ^(.*)$ /public/user/$1 [L]

Also, do I have to modify the html href like this <a href="/settings.php">?

Yes, the rule only works in one direction, regex pattern to target. Never the other way around. So you need to make sure your content has the URLs that you want to use.

这篇关于从mod_rewrite的URL删除两个子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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