Clean URL assistance php/.htaccess

2019-09-21 10:01发布

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

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"

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 .. 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...

标签: php .htaccess
1条回答
地球回转人心会变
2楼-- · 2019-09-21 10:25

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

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

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]
查看更多
登录 后发表回答