Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.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将特定slug重写到特定页面_.htaccess_Mod Rewrite_Url Rewriting - Fatal编程技术网

.htaccess htaccess将特定slug重写到特定页面

.htaccess htaccess将特定slug重写到特定页面,.htaccess,mod-rewrite,url-rewriting,.htaccess,Mod Rewrite,Url Rewriting,我目前正在构建一个简单的CMS,我想将特定的slug(如/setup和/admin)重写到它们各自的页面,同时允许htaccess将所有其余的请求重写到我的服务页面 以下是我现在在.htaccess中的内容: Options +FollowSymlinks -MultiViews RewriteEngine on #RewriteBase / # Rewrite the setup slug to the setup page RewriteCond %{REQUEST_URI} ^/setu

我目前正在构建一个简单的CMS,我想将特定的slug(如/setup和/admin)重写到它们各自的页面,同时允许htaccess将所有其余的请求重写到我的服务页面

以下是我现在在.htaccess中的内容:

Options +FollowSymlinks -MultiViews
RewriteEngine on
#RewriteBase /

# Rewrite the setup slug to the setup page
RewriteCond %{REQUEST_URI} ^/setup$
RewriteRule ^/setup$ /setup.php [L,R=301]

# Rewrite all other URL's to the keyhole that translates them
RewriteRule ^(.*)$ keyhole.php?slug=$1 [L,QSA]
目前,重定向适用于所有其他URL。它捕获每一个,然后将它们发送到keyhole.php。但是,当我尝试访问
http://localhost/basic-cms/setup
它将我重定向到
http://localhost/var/www/basic-cms/setup.php

另外,如果我尝试转到
http://localhost/basic-cms/setup.php
然后我就到了
http://localhost/setup.php

另外,当我取消对RewriteBase的注释时,我仍然有同样的问题,然后它会打断我的另一条包罗万象的重写行

编辑:

我从给出的答案中获取了提供的信息,并拥有一个新的.htaccess文件:

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /

# Rewrite the setup slug to the setup page
RewriteCond %{REQUEST_URI} ^setup$
RewriteRule ^setup$ setup.php [L]

# Rewrite all URL's to the keyhole that translates them
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ basic-cms/keyhole.php?slug=$1 [L,QSA]
然而,虽然我不再从setup重定向中获得奇怪的重定向,但它似乎仍然不会将setup转到setup.php。它的行为就像跳过了检查^setup$的RewriteCond。我想知道这是否与我新建立的基地有关

最终编辑:

好吧,在细细咀嚼之后,我发现我错在哪里了。这是我搞砸的条件,因为我已经在等式中添加了基数。下面是working.htaccess,它允许我将所有slug重定向到keyhole.php,setup slug除外,它将直接转到setup.php:

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /

# Rewrite the setup slug to the setup page
RewriteCond %{REQUEST_URI} ^/basic-cms/setup$
RewriteRule ^(.*)$ basic-cms/setup.php [L]

# Rewrite all URL's to the keyhole that translates them
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ basic-cms/keyhole.php?slug=$1 [L,QSA]

显然,由于我正在构建一个本地Apache实例,而且我还没有设置虚拟主机,所以当我将这个htaccess文件移动到一个真正的URL时,它会被稍微修剪一下,因此它不包括路径的“基本cms”部分,由于该目录将不可避免地被实时web服务器上的根目录删除。

您需要的是一个内部重定向,但是根据您的规则,您有
R=301
删除该目录,只保留
[L]

您可能还希望在keyhole.php规则中添加以下条件:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
避免将现有页面或目录重定向到自身,但这取决于您将如何使用CMS

在CMS中,WordPress base
.htaccess
注意他们如何使用上述内容:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

这帮了大忙,谢谢!现在,它似乎完全跳过了^setup$的RewriteCond。我想这可能就是我的情况。如果我在你有机会回复之前就发现了,我会添加一个编辑来描述我的错误,并将此标记为答案。找到了。我刚刚把我的状况搞砸了。现在一切都很好。谢谢你的帮助@SamHuckaby很高兴你能找到答案,我的帖子对你有帮助