Php 如何使用htaccess modrewrite重写URL

Php 如何使用htaccess modrewrite重写URL,php,apache,.htaccess,mod-rewrite,url-rewriting,Php,Apache,.htaccess,Mod Rewrite,Url Rewriting,对不起,如果这真的很简单!我没有htaccess重写的经验,所以我只是摸索一下 我有这个网址 /category.php?cat=projects/retail/ 但我想将其重写为一个干净的URL,因此它看起来如下所示: /category/projects/retail/ 我写了这样一篇文章: <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^category.php/([a-zA-Z0-9-/]+)$ /ca

对不起,如果这真的很简单!我没有htaccess重写的经验,所以我只是摸索一下

我有这个网址

  /category.php?cat=projects/retail/
但我想将其重写为一个干净的URL,因此它看起来如下所示:

  /category/projects/retail/
我写了这样一篇文章:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^category.php/([a-zA-Z0-9-/]+)$ /category?s=$1 [L]
</IfModule>
定义原始URL,我认为
([a-zA-Z0-9-/]+)
中的内容表示匹配大写、小写和连字符

我还知道
[L]
告诉服务器停止

我不知道的是如何重写URL-还没有


有人能帮忙吗?

您可以在
文档\u ROOT/.htaccess
文件中使用此代码:

RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+category\.php\?cat=([^\s&]+) [NC]
RewriteRule ^ /category/%1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^category/(.+?)/?$ category.php?cat=$1 [L,QSA,NC]

怎么卡住的?你试过了吗?你有错误吗?谢谢你,成功了!我只需要使用“从漂亮的URL到实际的URL的内部转发”,非常感谢!不客气,很高兴事情解决了。
RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+category\.php\?cat=([^\s&]+) [NC]
RewriteRule ^ /category/%1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^category/(.+?)/?$ category.php?cat=$1 [L,QSA,NC]