[英]Can I use PHP and/or htaccess to rewrite a URL but not redirect?
Is there a way I can use PHP (and/or .htaccess) to rewrite the URL of a page. 有没有办法可以使用PHP(和/或.htaccess)来重写页面的URL。
For example if a user goes to www.mysite.com/french they are actually accessing the page which is www.mysite.com/index.php?lang=fr 例如,如果用户访问www.mysite.com/french,他们实际上正在访问www.mysite.com/index.php?lang=fr页面
But not a redirect. 但不是重定向。
You want to use mod_rewrite
and an .htaccess file to achieve this. 您想使用
mod_rewrite
和.htaccess文件来实现此目的。
RewriteEngine On
RewriteBase /
RewriteRule ^french/(.*)$ /index.php?lang=fr [L,QSA]
Yes, using Apache mod_rewrite and appropriate rules in an .htaccess file. 是的,在.htaccess文件中使用Apache mod_rewrite和适当的规则。
The docs on mod_rewrite are here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html mod_rewrite上的文档在这里: http : //httpd.apache.org/docs/current/mod/mod_rewrite.html
on the Apache site you can find several examples of URL rewriting flavors , by the way it's enough to use something like this in an .htaccess file: 在Apache站点上,你可以找到几个URL重写风格的例子,就像它在.htaccess文件中使用这样的东西一样:
RewriteEngine on
RewriteRule ^french(/|)$ /index.php?lang=fr [flag]
Where [flag]
can be one of the following: 其中
[flag]
可以是以下之一:
[PT]
[L,PT]
[QSA]
[L,QSA]
You may want to have a look at the PT (passthrough) flag docs or RewriteRule flags docs . 您可能想要查看PT(passthrough)标志文档或RewriteRule标志文档 。
Also, pay attention to what your links are pointing to: in fact, the RewriteRule first argument is a regular expression that will be used to match the URLs to be rewritten. 另外,请注意链接指向的内容:实际上,RewriteRule第一个参数是一个正则表达式,用于匹配要重写的URL。 At the moment,
在这一刻,
^french(/|)$
matches "french", right after the domain name,followed either by a slash (/) or nothing (that's the meaning of (/|)
); 在域名之后匹配“french”,后跟斜杠(/)或者没有(这是
(/|)
的含义); that is, it'll match www.mysite.com/french and www.mysite.com/french/ but nothing else. 也就是说,它将匹配www.mysite.com/french和www.mysite.com/french/,但没有别的。 If you need to parse query string arguments, or subpaths, then you may need a more complex regex.
如果需要解析查询字符串参数或子路径,则可能需要更复杂的正则表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.