重定向和重写与mod_rewrite的重写、重定向、mod_rewrite

2023-09-02 00:38:47 作者:帅裂天际

在问这个问题:干净的URL进行搜索查询我尝试了用mod_rewrite的:

After asking this question: Clean URL's for search query? I tried something with mod_rewrite:

RewriteCond %{QUERY_STRING} ^s=([a-z]+)$ [NC]
RewriteRule / /s/$1? [NC,R=301,L]

RewriteRule ^s/([a-z]+)$ /?s=$1 [NC,L]

有什么目标?

What's the goal?

重定向 http://example.com/?s=query 到的 http://example.com/s/query 在重写 http://example.com/s/query 到的 http://example.com/?s=query Redirect http://example.com/?s=query to http://example.com/s/query Rewrite http://example.com/s/query to http://example.com/?s=query

这看起来像是双重的工作,但如果你好好看看,你看看我尽量做到:

This looks like double work but if you take a good look you see what I try to accomplish:

重定向任何搜索查询字符串来清洁当量(无论是形式,或某人键入它直接) 在重写(不定向),其URL回到动态查询字符串,这样我可以通过$ _ GET 让它使用PHP

如果我想它这个样子应该是可能的。 所以我想寻求有经验的mod-rewrite的帮助,帮我出这一点。

If I think about it like this it should be possible. So I would like to seek the help of the experienced mod rewriter to help me out with this one.

2号作品,但仅此而已。

Number 2 works but that's it.

推荐答案

这应该工作,我有一些不同的名称和迪尔斯对其进行了测试,但应该确定你的情况。

This should work, I tested it with some different names and dirs, but that should be ok in your case.

注:用于匹配的组从您必须使用的RewriteCond %1 不是 $ 1

NB: for matched group from the RewriteCond you must use %1 not $1.

RewriteCond %{QUERY_STRING} ^s=([a-z]+)$ [NC]        
RewriteRule ^$ /s/%1? [NC,R,L]                     

RewriteRule ^s/([a-z]+)$ /?s=$1 [NC,L] 

编辑调试(见注释):

Edit for debug (see comments) :

我的测试方法是:

| /
| --> doc
|   |
|   --> doc.php (takes doc as GET parameter)
|     | index.php

我的Apache重写为

My apache rewrite is

RewriteCond %{QUERY_STRING} ^doc=([a-z]+)$ [NC]
RewriteRule ^$ /doc/%1? [NC,R,L]

RewriteRule ^doc/([a-z]+)$ /doc/doc.php?doc=$1 [NC,L]

然后要求domain.com/?doc=query显示器 DOC是查询

我的作品。