Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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
.htaccess htaccess mod重写混乱_.htaccess_Mod Rewrite - Fatal编程技术网

.htaccess htaccess mod重写混乱

.htaccess htaccess mod重写混乱,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我正试图将mod重写应用到我的URL中,以使其更好,但我陷入了这个主题的所有困惑之中 请你帮我举几个我想做的例子: 我想将站点根目录中的任何变量附加到lyprofile.php页面中 # Turn mod_rewrite on RewriteEngine On RewriteCond %{THE_REQUEST} /(.*) RewriteRule /(.*) lyprofile.php?us=$1 我想要一个url,比如profile/settings,以转到lysettings.php

我正试图将mod重写应用到我的URL中,以使其更好,但我陷入了这个主题的所有困惑之中

请你帮我举几个我想做的例子:

我想将站点根目录中的任何变量附加到lyprofile.php页面中

# Turn mod_rewrite on
RewriteEngine On

RewriteCond %{THE_REQUEST} /(.*)
RewriteRule /(.*) lyprofile.php?us=$1
我想要一个url,比如profile/settings,以转到lysettings.php

# Turn mod_rewrite on
RewriteEngine On

RewriteCond %{THE_REQUEST} /profile/settings
RewriteRule /profile/settings lysettings.php
这两个例子应该可以帮助我计算出所有其他的URL,但是我不能让它们工作


你还需要一个绝对URL吗,因为我在本地机器上工作,绝对URL会引起很多麻烦。以防我的绝对URL是
http://localhost/Linku2.5/

您能在vhosts配置中检查这一点吗

# Turn mod_rewrite on
RewriteEngine On

RewriteRule lyprofile\.php - [L]

RewriteCond %{REQUEST_URI} /(.*)
RewriteRule /(.*) lyprofile.php?us=$1 [L]

RewriteCond %{REQUEST_URI} /profile/settings
RewriteRule /profile/settings lysettings.php [L]

您通常希望从最具体的规则转到最不具体的规则。在您想要做的两件事中,第一件是最不具体的,因为
(.*)
可以是任何东西。所以我们必须从更具体、更少随意的
/profile/settings
开始

如果这些规则在htaccess文件中(在您的情况下,在
Link2.5
目录中),则不需要前导斜杠,因此只需:

# you only need to turn on once
RewriteEngine On

# Don't need absolute URLs, but you may need this line:
RewriteBase /Link2.5/

RewriteRule ^profile/settings lysettings.php [L]
然后,因为您的另一个规则非常通用,所以您需要添加一些条件,以便不会导致无限循环(重写引擎将继续循环,直到URI停止更改):

如果您知道您的“us”变量只能是数字或字母,那么您可以将规则的行设置得更具体一些:

RewriteRule ^([A-Za-z0-9]+)$ lyprofile.php?us=$1 [L,QSA]

等等。

我已经反复尝试过了,它只是说找不到url;我做错了什么@JackTrowbridge确保一切都在正确的位置,确保mod_rewrite已打开,确保您的htaccess文件正在读取,确保您可以访问目标(例如lysettings.php和lyprofile.php),检查您的错误日志,我有,但我认为还有另一个问题使此方法对我毫无用处。当我让它与其他代码一起工作时,比如说,我有一个url“profile/settings”,它重定向到lysettings.php;问题是在lysettings.php中,它的行为就像它有url配置文件/设置一样,因此所有样式表和链接都找不到,因为它们是从根目录引用的;它不重定向它基本上充当url?这正常吗?@JackTrowbridge“它不重定向它基本上充当url?这正常吗?”这就是“重写”的含义,它完全在服务器端,它不同于“重定向”这是mod_重写的目的。至于css内容,您的内容中有相对URI,将它们全部更改为绝对值或在页面标题中添加
或其他内容
RewriteRule ^([A-Za-z0-9]+)$ lyprofile.php?us=$1 [L,QSA]