php htaccess初学者指南

php htaccess初学者指南,php,.htaccess,Php,.htaccess,我刚刚开始学习htaccess 我想测试一个页面: 我有一个名为“favs”的文件夹,里面有一个php文件索引,并从中传递到带有querystring ID的页面gallery.php 因此,url将是: mainsite.com/favs/gallery.php?id=7 现在,我希望url类似于: mainsite.com/favs/7 这是我在.htaccess文件中编写的代码: <IfModule mod_rewrite.c> RewriteEngine On Rew

我刚刚开始学习htaccess

我想测试一个页面:

我有一个名为“favs”的文件夹,里面有一个php文件索引,并从中传递到带有querystring ID的页面gallery.php

因此,url将是:

mainsite.com/favs/gallery.php?id=7
现在,我希望url类似于:

mainsite.com/favs/7
这是我在.htaccess文件中编写的代码:

<IfModule mod_rewrite.c>


RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ gallery.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ gallery.php?id=$1

</IfModule>

重新启动发动机
重写规则^([a-zA-Z0-9_-]+)$gallery.php?id=$1
重写规则^([a-zA-Z0-9_-]+)/$gallery.php?id=$1

您的规则是错误的。你有这个:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ gallery.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ gallery.php?id=$1
</IfModule>
上面的代码是您应该放在.htaccess中的代码,如果它在您的根目录中。如果它位于/favs目录中,则需要从RewriteRule中删除
/favs
,并添加一个
RewriteBase
,如下所示:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^favs/([0-9]+)/?$ favs/gallery.php?id=$1
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /favs
RewriteRule ^([0-9]+)/?$ gallery.php?id=$1
</IfModule>

重新启动发动机
基础设施/设施
重写规则^([0-9]+)/?$gallery.php?id=$1
注意:根据您的问题,我已经从正则表达式中删除了字母、
\uu
-
。如果需要匹配它们,可以相应地调整上述规则。

这是我的完整代码:

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



RewriteEngine On
RewriteRule ^favs/([0-9]+)/?$ favs/gallery.php?id=$1

</IfModule>

你的问题是什么?重写不起作用吗?它的行为是否与您想要的有所不同?您好,当我将url用作mainsite.com/favs/7时,它不起作用。它应该显示mainsite.com/favs/gallery.php页面?id=7但它显示页面不存在。听起来您的根文件夹中有这个.htaccess,但希望重定向到favs/gallery.php。我已相应地编辑了我的答案;请再试一次。嗨,很抱歉给您添麻烦,但它仍然不起作用。。让我向您展示整个代码:这是我的url:这是我的代码请澄清“不工作”--这在我的测试服务器上运行正常。另外,请确认页面
/favs/gallery.php
确实存在。如果没有,你就不能重定向到它;如果你尝试的话,你会得到404。请检查我发布的全部代码。是的,画廊页面存在这里是url
http://www.fullridescholarships.net/favs/