Mod-rewrite url from /example.php?id=1 to /example/title-of-page

强颜欢笑 提交于 2019-12-20 06:17:39

问题


I have a website that generates urls like:

www.site.com/example.php?id=1

I would like that url to be displayed like:

www.site.com/example/the-title-of-the-page-whatever-it-may-be

Can I do this in .htaccess or do I need to edit the php or what?

Be gentle, and treat me like an idiot please - I'm brand new to all this :)


回答1:


If your using a apache server you can use mod rewrite




回答2:


Use this for your htaccess :

RewriteEngine On
RewriteRule ^([^/]*).html$ /example.php?id=$1 [L]

Your Old URL > example.com/example.php?id=1
Will be change to > example.com/1.html

Using id :

RewriteEngine On
RewriteRule ^id/([^/]*).html$ /example.php?id=$1 [L]

Your Old URL > example.com/example.php?id=1
Will be change to > example.com/id/1.html

Hope Help you ;)




回答3:


You indeed need an .htaccess file for this and of course some PHP code to see what page needs to be loaded. The .htaccess could look something like this:

# Start rewrite engine
RewriteEngine On

# Catch non existing files and/or folders (treat as optimized urls)
RewriteCond %{REQUEST_FILENAME} !\.(jpg|jpeg|gif|png|css|js|pl|txt)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Catch blocked folders (treat as optimized urls)
RewriteRule ^(.*)$ index.php [L]


来源:https://stackoverflow.com/questions/7121620/mod-rewrite-url-from-example-phpid-1-to-example-title-of-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!