.htaccess 使用htaccess启动时遇到问题

.htaccess 使用htaccess启动时遇到问题,.htaccess,mod-rewrite,url-rewriting,.htaccess,Mod Rewrite,Url Rewriting,我一直在尝试实现一个mod重写来清理我的URL。我以前从未这样做过 以下是我的一个URL的示例: http://www.example.com/listings.php?category=Accident%20and%20Crash%20Repairs 文件listings.php位于我的站点的根级别 我已经测试了我的服务器上是否启用了mod rewrite,并发现它工作正常 从一个空白文件开始,我试图创建一个.htaccess文件来重写我的URL。以下是我迄今为止所尝试的: RewriteE

我一直在尝试实现一个mod重写来清理我的URL。我以前从未这样做过

以下是我的一个URL的示例:

http://www.example.com/listings.php?category=Accident%20and%20Crash%20Repairs
文件listings.php位于我的站点的根级别

我已经测试了我的服务器上是否启用了mod rewrite,并发现它工作正常

从一个空白文件开始,我试图创建一个.htaccess文件来重写我的URL。以下是我迄今为止所尝试的:

RewriteEngine On
RewriteRule ^([^/]*)\.html$ /listings.php?category=$1 [L]

RewriteEngine On
RewriteRule ^/listings.html$ /listings.php?category=Accident%20and%20Crash%20Repairs [L]
在下一个示例中,我只是尝试获取url要更改的部分,这大致基于对另一个堆栈溢出查询的回答

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteBase /
 RewriteRule    ^lineage/?$    listings.php   [NC,L]
</IfModule>
但如果不是,我会满足于根本看不到变量

有人能指出我哪里出了问题,或者以任何方式帮助我解决这个问题吗


非常感谢。

通过
httpd.conf
启用mod_rewrite和.htaccess,然后将此代码放在
文档根目录下的
.htaccess
中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+listings/[^\?\s%20]+\s [NC]
RewriteRule ^listings/([^-]+)-([^-]+)(.*)$ listings/$1\%20$2$3 [L,NC]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+listings/[^\?\s%20]+\s [NC]
RewriteRule ^listings/([^-]+)/?$ listings.php?category=$1 [L,NC,QSA]

RewriteCond %{QUERY_STRING} ^(category)=([^%20]+)%20([^%20]+)(.*)$ [NC]
RewriteRule ^(listings\.php)/?$ $1?%1=%2-%3%4 [R=302,L,NC,NE]

RewriteCond %{QUERY_STRING} ^category=([^\s%20]+)$ [NC]
RewriteRule ^(listings)\.php/?$ $1/%1? [R=302,L,NC,NE]
一旦此基于递归的规则就位,
/listings/incident and crash repairs
的URI将转发到
/listings.php?category=incident%20和%20crash%20 repairs

/listings.php?category=incident%20和%20crash%20repairs
的URI将从外部重定向到
/listings/incident And crash repairs


验证其工作后,将
R=302
更改为
R=301
通过
httpd.conf
启用mod_rewrite和.htaccess,然后将此代码放入
文档根目录下的
.htaccess

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+listings/[^\?\s%20]+\s [NC]
RewriteRule ^listings/([^-]+)-([^-]+)(.*)$ listings/$1\%20$2$3 [L,NC]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+listings/[^\?\s%20]+\s [NC]
RewriteRule ^listings/([^-]+)/?$ listings.php?category=$1 [L,NC,QSA]

RewriteCond %{QUERY_STRING} ^(category)=([^%20]+)%20([^%20]+)(.*)$ [NC]
RewriteRule ^(listings\.php)/?$ $1?%1=%2-%3%4 [R=302,L,NC,NE]

RewriteCond %{QUERY_STRING} ^category=([^\s%20]+)$ [NC]
RewriteRule ^(listings)\.php/?$ $1/%1? [R=302,L,NC,NE]
一旦此基于递归的规则就位,
/listings/incident and crash repairs
的URI将转发到
/listings.php?category=incident%20和%20crash%20 repairs

/listings.php?category=incident%20和%20crash%20repairs
的URI将从外部重定向到
/listings/incident And crash repairs


一旦您验证了它的工作状态,将
R=302
更改为
R=301

如果您加载
http://www.example.com/lineage/
http://www.example.com/listings.html
http://www.example.com/Accident%20and%20Crash%20Repairs.html
?你有404页吗?@Gerben谢谢你的评论。没有404页。页面看起来很好,只是没有任何结果。所以
http://www.example.com/listings.php?category=Accident%20and%20Crash%20Repairs
加载你的页面,所有结果都很好?@anubhava Yeah效果很好。这可能不是一个很好的系统,但我只是在学习。唯一的问题是重新编写URL。如果加载
http://www.example.com/lineage/
http://www.example.com/listings.html
http://www.example.com/Accident%20and%20Crash%20Repairs.html
?你有404页吗?@Gerben谢谢你的评论。没有404页。页面看起来很好,只是没有任何结果。所以
http://www.example.com/listings.php?category=Accident%20and%20Crash%20Repairs
加载你的页面,所有结果都很好?@anubhava Yeah效果很好。这可能不是一个很好的系统,但我只是在学习。唯一的麻烦是重新编写URL。非常感谢您的回答。我现在正在看一看。我会让你知道我的进展。在看了你的答案并修改了我的.htaccess文件以适应之后,不幸的是我仍然无法让mod_rewrite工作。站点重定向301在添加到文件中时可以正常工作,但不能重写mod_。奇怪!我的主机公司坚持所有服务器都启用mod_rewrite。非常感谢你的回答。你做了什么没用的URI?你能提供一些细节,比如什么不起作用?你得到404或what>了吗?最后,请在你的问题中发布你当前的.htaccess。如果我在地址栏中输入url,那么我将进入正确的页面。所以这个答案是正确的。对不起,我弄错了。不过,我认为我的问题现在已经略有改变。我需要得到点击的链接,使我的网页与重写的网址。目前它仍然需要我去哦,这应该很容易处理。只需将
R=301
添加到上面的最后一条重写规则中(参见编辑后的答案)。非常感谢您的回答。我现在正在看一看。我会让你知道我的进展。在看了你的答案并修改了我的.htaccess文件以适应之后,不幸的是我仍然无法让mod_rewrite工作。站点重定向301在添加到文件中时可以正常工作,但不能重写mod_。奇怪!我的主机公司坚持所有服务器都启用mod_rewrite。非常感谢你的回答。你做了什么没用的URI?你能提供一些细节,比如什么不起作用?你得到404或what>了吗?最后,请在你的问题中发布你当前的.htaccess。如果我在地址栏中输入url,那么我将进入正确的页面。所以这个答案是正确的。对不起,我弄错了。不过,我认为我的问题现在已经略有改变。我需要得到点击的链接,使我的网页与重写的网址。目前它仍然需要我去哦,这应该很容易处理。只需在上面的最后一条重写规则中添加
R=301
(参见编辑后的答案)。