mod_rewrite,/ index . php ?p =测试/测试

时间:2021-08-06 11:18:50

i'm trying to set up a very simple rule in .htaccess file in order to rewrite this kind of url: www.domain.com/index.php?page=test to this: www.domain.com/test

我试图在.htaccess文件中设置一个非常简单的规则,以便重写这种url: www.domain.com/index.php?页面=测试:www.domain.com/test

I'm newbie to mod_rewrite, and so far, i came to this rule

我是mod_rewrite的新手,到目前为止,我已经习惯了这个规则。

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d


RewriteRule ^(.*)$ index.php?$1

but it doesn't work.

但它不工作。

Any help? Thanks a lot

任何帮助吗?非常感谢

2 个解决方案

#1


4  

Almost right, you just forgot the page=:

几乎是对的,你只是忘记了页面=:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1

Now note that this rule does the exact opposite: It rewrites requests of paths like /test internally to /index.php?page=test and not vice versa.

现在请注意,这条规则做了完全相反的事情:它重写了内部的路径请求,比如/测试到/index.php?页面=测试,反之亦然。

#2


0  

Another approach is:

另一种方法是:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^([a-zA-Z0-9-_]+)/?$ index.php?page=$1.php [NC,L]

This also ensures your URL can only contain letters, numbers, dashes and underscores.

这也确保你的URL只能包含字母、数字、破折号和下划线。

Altough, on a sidenote, I'd recommend you to have separate files for each page and including a header.php and a footer.php in those...

在sidenote上,我建议您对每个页面都有单独的文件,包括一个标题。php和一个页脚。php在那些……

#1


4  

Almost right, you just forgot the page=:

几乎是对的,你只是忘记了页面=:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1

Now note that this rule does the exact opposite: It rewrites requests of paths like /test internally to /index.php?page=test and not vice versa.

现在请注意,这条规则做了完全相反的事情:它重写了内部的路径请求,比如/测试到/index.php?页面=测试,反之亦然。

#2


0  

Another approach is:

另一种方法是:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^([a-zA-Z0-9-_]+)/?$ index.php?page=$1.php [NC,L]

This also ensures your URL can only contain letters, numbers, dashes and underscores.

这也确保你的URL只能包含字母、数字、破折号和下划线。

Altough, on a sidenote, I'd recommend you to have separate files for each page and including a header.php and a footer.php in those...

在sidenote上,我建议您对每个页面都有单独的文件,包括一个标题。php和一个页脚。php在那些……