简体   繁体   English

如何使用 .htaccess 创建干净的 url

[英]How to create clean url using .htaccess

This is my .htaaccess code.这是我的 .htaac​​cess 代码。

RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$ movie.php?name=$1
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.我不清楚干净的 url 概念。 Is there any codings I ve to write on my php page.有没有我要在我的 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谢谢

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=$1 [L,QSA]

If you're want to have clean URLs, then you have to use clean URLs in your html code, eg 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您的重写规则将采用在服务器上并不真正存在的地址(毕竟您实际上没有包含titanic文件的/movie目录),并将其转换为实际的

/movie.php?name=titanic

The user would never see this url, however, because the rewrite would take place purely within Apache.然而,用户永远不会看到这个 url,因为重写将完全在 Apache 中进行。 As long as the HTML you send to the user contains only "clean" urls, they'll never see the query strings.只要您发送给用户的 HTML 只包含“干净”的 url,他们就永远不会看到查询字符串。

RewriteEngine on
RewriteRule ^movie/([0-9a-zA-Z]+)$ movie.php?name=$1 [L]

Add above code in .htaccess file在 .htaccess 文件中添加以上代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM