php 如何使用 .htaccess 创建干净的 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18851994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create clean url using .htaccess
提问by Rakesh
This is my .htaaccess code.
这是我的 .htaaccess 代码。
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$ movie.php?name=
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.in$
RewriteRule ^/?$ "http\:\/\/example\.in" [R=301,L]
ErrorDocument 404 /404.php
AddDefaultCharset UTF-8
Header unset ETag
FileETag None
I need clean url for my website. I've referred lots of tutorials and forums and created the above code. But not working.. Almost I'm fighting with code. I dont understand the clean url concept clearly. Is there any codings I ve to write on my php page.
我的网站需要干净的网址。我参考了很多教程和论坛并创建了上面的代码。但不工作.. 几乎我正在与代码作斗争。我不清楚干净的 url 概念。有没有我要在我的 php 页面上写的代码。
<a href='movie.php?name=titanic'> Titanic </a>
I've this link in my index.php file.
我的 index.php 文件中有这个链接。
I want example.in/movie/titanic
while click the link Titanic.
我想要example.in/movie/titanic
点击链接泰坦尼克号。
Also I want to get the value by $_[request].
我也想通过 $_[request] 获取值。
What exactly I've to do. Please dont make this question as duplicate, I've searched a lot and didn't got the concept clearly. Please help me out.
我到底要做什么。请不要让这个问题重复,我已经搜索了很多,但没有清楚地了解这个概念。请帮帮我。
Thanks
谢谢
回答by anubhava
Replace your code with this:
用这个替换你的代码:
ErrorDocument 404 /404.php
AddDefaultCharset UTF-8
Header unset ETag
FileETag None
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+movie\.php\?name=([^\s&]+) [NC]
RewriteRule ^ movie/%1? [R=301,L]
RewriteRule ^movie/([^/]+)/?$ movie.php?name= [L,QSA]
回答by Marc B
If you're want to have clean URLs, then you have to use clean URLs in your html code, e.g. use
如果你想拥有干净的 URL,那么你必须在你的 html 代码中使用干净的 URL,例如使用
<a href="/movie/titanic">Titanic</a>
Your rewrite rules will take that doesn't-really-exist-on-the-server address (you don't really have a /movie
directory that contains a titanic
file, after all), and translates it in to the actual
您的重写规则将采用在服务器上并不真正存在的地址(毕竟您实际上没有/movie
包含titanic
文件的目录),并将其转换为实际的
/movie.php?name=titanic
The user would never see this url, however, because the rewrite would take place purely within Apache. As long as the HTML you send to the user contains only "clean" urls, they'll never see the query strings.
然而,用户永远不会看到这个 url,因为重写将完全在 Apache 中进行。只要您发送给用户的 HTML 只包含“干净”的 url,他们就永远不会看到查询字符串。
回答by Ashish
RewriteEngine on
RewriteRule ^movie/([0-9a-zA-Z]+)$ movie.php?name= [L]
Add above code in .htaccess file
在 .htaccess 文件中添加以上代码