清洁网址援助的PHP /的.​​htaccess清洁、网址、htaccess、PHP

2023-09-02 01:14:30 作者:斗酒纵马

我希望做一些更清洁的URL,这样就基本,网址看起来更好。

I'm looking to make some cleaner urls so that it basically, the web address looks nicer.

目前我的网址是这样的.. ... co.uk/article.php?sport=football&id=23 ..&放大器;标题后可能..呃..这是标题

currently my url looks like this .. ...co.uk/article.php?sport=football&id=23.. & title for the post could be.. erm .."this is the title"

我一直在寻找各地的网络和各地的YouTube试图找到一个视频来帮助我几乎不参加,当我读到的东西的任何信息,因此视频看起来更富有成效我。我想是我的网址看像...

i have been looking around the web and all over youtube trying to find a video to help as i hardly take any info in when i read stuff, so videos seems more productive for me. What i would like is my url to read something like...

... co.uk/sport/football/this-is-the-title ..如果它是另一个话题是这样... ... co.uk/sport/tennis/this-is-another-title ... 东西不错,干净,告诉人们他们正在寻找的到底是什么。此外,我想拥有它,所以当它的网页上,他们的网页不必须有有扩展添加上,这样你不必把的.php 的.html 上的地址的结束,以及...

...co.uk/sport/football/this-is-the-title .. and if it was another topic something like... ...co.uk/sport/tennis/this-is-another-title ...something nice and clean and tells people exactly what they're looking at. Also, i would like to have it so when its on pages, them pages dont have to have there extension added on so you dont have to put .php or .html on the end of the address as well...

推荐答案

您必须保留ID也像#1的URL结构在这里pretty的URL。因此,而不是: /体育/足球/这不是采用的标题有它喜欢的:

You must keep id also in the pretty URL like the URL structure of Stackoverflow here. So instead of: /sport/football/this-is-the-title have it like:

/sport/football/23/this-is-the-title

一旦得到解决现在让我们来看看建立了mod_rewrite code来实现它。

Once that is settled let's now look into establishing mod_rewrite code to achieve it.

的httpd.conf 启用了mod_rewrite和.htaccess,然后把这个code在的.htaccess DOCUMENT_ROOT 目录:

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^sport/([^/]+)/([^/]+)/([^/]+)/?$ /article.php?sport=$1&id=$2&title=$3 [L,QSA]