我目前正在构建一个简单的CMS,我想将特定的slugs(如/ 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]
目前,重定向适用于所有其他网址。它捕获每个,然后将它们发送到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.php。它就像跳过检查^ setup $的RewriteCond一样。我想知道它是否与我新建立的RewriteBase有关...
最终修改:
好的,在咀嚼了一会儿之后,我弄明白我哪里出错了。这是我搞砸了的条件,因为我已经将RewriteBase添加到等式中。因此,对于记录,这里是工作.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实例,并且我还没有设置虚拟主机,所以当我将它移动到真实URL时,这个htaccess文件会被修剪一下,所以它不包括路径的“basic-cms”部分,因为该目录将不可避免地由实时Web服务器上的根目录。
答案 0 :(得分:1)
您想要的是内部重定向,但是在您的规则中, R=301
将其删除并仅保留 [L]
您可能还想在keyhole.php规则中添加以下条件:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
避免将现有网页或目录重定向到自身,但这取决于您将如何使用CMS。
从CMS,WordPress基础.htaccess
注意他们如何使用上述内容:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]