mod_rewrite规则删除".php"扩展和重写查询字符串 [英] mod_rewrite rule to remove ".php" extension and rewrite query string

查看:69
本文介绍了mod_rewrite规则删除".php"扩展和重写查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是Mod_Rewrite的新手,并且想首次使用它,我有一个简单的请求,如何从页面中删除.php扩展名并重写querystring.

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

类似aboutus.php?page = moreinfo和product.php?page = second-product以及更多.

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

我只需要一个简单的规则即可从页面中删除.php扩展名,并为我的所有页面重写get page查询字符串. 有谁能帮忙吗?

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 ?

推荐答案

在您的htaccess中尝试此操作(应该在您的myproject目录中):

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]

myproject目录中与该规则匹配的每个URL(例如something/other-thing)都将被重写为something.php?page=other-thing

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

这篇关于mod_rewrite规则删除".php"扩展和重写查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆