htaccess的外部重写/内部重定向重写、重定向、htaccess

2023-09-02 00:44:48 作者:大风起兮裙飞扬

有两件事情,我想实现用.htaccess文件。 第一个是

There are two things that I would like to achieve with .htaccess file. The first one is

www.hostname.com/index.php?question  -> www.hostname.com/question
www.hostname.com/index.php?myinfo  -> www.hostname.com/myinfo
www.hostname.com/index.php?notification  -> www.hostname.com/notification

所以我使用外部改写重新EX preSS的网址如下。

so I use external rewrite to re-express on the URL as following.

RewriteCond %{THE_REQUEST} /(index.php)?([^&]+) HTTP
RewriteRule ^ /%2? [R=301,L]

现在上面的说法正确显示我想要的。问题是在内部转换回当条件满足。条件是,如果%{THE_REQUEST} 等于后的任何字符 /

Now the above statement correctly displays as I want. The problem is to internally convert back when a condition is satisfied. The condition is if %{THE_REQUEST} is equal to any character after '/'.

RewriteCond %{REQUEST_URI} ^/(.*)$ [NC]
RewriteRule ^(.*)$ index.php?$1 [NC,L]

让我的PHP code可以识别 $ _ GET 参数。这里,即使该条件被满足,将不处理重写规则

So that my php code can recognize the $_GET parameter. Here, even though the condition is satisfied it will not process the RewriteRule.

第二个问题是

www.hostname.com/index.php?category=spo  -> www.hostname.com/category/spo
www.hostname.com/index.php?category=ite  -> www.hostname.com/category/ite
www.hostname.com/index.php?category=gam  -> www.hostname.com/category/gam

转换使用exteral重写完成的:

The conversion is completed using exteral rewrite:

RewriteCond %{THE_REQUEST} /(index)?category=([^&]+) HTTP
RewriteRule ^ category/%2? [R=301,L]

此外,我想转换回无论是写在URL回原来的格式在内部,所以我使用以下条件从previous情况来区分,

Again, I'd like to convert back whatever is written in the URL back to the original format internally so I use the following condition to differentiate from the previous case,

RewriteCond %{REQUEST_URI} ^/category/(.*)$
RewriteRule ^category/(.*)/?$ index.php?category=$1 [NC,L]

我的PHP code不能识别 $ _ GET 的参数和变量。当我使用htacces测试仪,它说,它应该工作,但事实并非如此。 http://martinmelin.se/rewrite-rule-tester/

my php code cannot recognize the $_GET parameter and variable. When I use htacces tester, it says it should work but it doesn't. http://martinmelin.se/rewrite-rule-tester/

我该如何解决这个问题?或者有没有更简单的方法来做到这一点?

How do I fix this problem? OR is there any easier way to accomplish this?

感谢您

推荐答案

请尝试以下规则:

RewriteEngine On

RewriteCond %{THE_REQUEST} s/index.php?category=([^&s]+) [NC]
RewriteRule ^ /category/%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^/]+)/?$ /index.php?category=$1 [NC,L,QSA]

RewriteCond %{THE_REQUEST} s/index.php?([^&s]+) [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?$1 [QSA,L]