mod_rewrite rule to remove “.php” extension and re

2019-09-20 04:33发布

Hello i an new to Mod_Rewrite and want to use it for the first time, I have one simple request, How do i remove the .php extension from my page and rewrite querystring.

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

i have multiple pages like this and what i need to do is

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

Likewise aboutus.php?page=moreinfo and product.php?page=second-product and more.

All i need is a simple rule to remove the .php extension from the page and rewrite get page query string for all of my pages. if any one can help ?

1条回答
家丑人穷心不美
2楼-- · 2019-09-20 04:48

Try this in your htaccess (which should be in your myproject directory):

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]

Every urls, inside your myproject directory, matching that rule (for example something/other-thing) will be rewritten to something.php?page=other-thing

查看更多
登录 后发表回答