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

2019-03-06 05:51发布

问题:

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]