mod_rewrite rule to remove “.php” extension and rewrite query string

大城市里の小女人 提交于 2019-12-14 03:26:42

问题


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:


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



来源:https://stackoverflow.com/questions/21364419/mod-rewrite-rule-to-remove-php-extension-and-rewrite-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!