mod_rewrite规则删除".php"扩展名并重写查询字符串



你好,我是新的mod_rewrite,想首次使用它,我有一个简单的请求,如何从页面上删除.php扩展名并重写querystring。

http://localhost/myproject/aboutus.php?page=info
http://localhost/myproject/products.php?page=first-product

我有这样的多页,我需要做的是

http://localhost/myproject/aboutus/info
http://localhost/myproject/products/first-product

同样关于us.php?page = moreinfo and product.php?page =第二产物等。

我所需要的只是一个简单的规则,可以从页面上删除.php扩展名,然后重写我所有页面的"获取页面查询"字符串。如果有人可以提供帮助?

在您的HTACCESS中尝试一下(应该在您的myproject目录中):

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /myproject/
RewriteCond %{ENV:REDIRECT_END} !^1$
RewriteRule ^([A-Za-z-_]+).php$ $1/%{QUERY_STRING} [R=301,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]
RewriteRule ^([A-Za-z-_]+)/([A-Za-z-_]+)/?$ $1.php?page=$2 [E=END:1,L]
RewriteRule ^([A-Za-z-_]+)/?$ $1.php [E=END:1,L]
RewriteRule ^$ index.php [E=END:1,L]

每个URL,在您的myproject目录中,将该规则(例如something/other-thing)匹配为something.php?page=other-thing

最新更新