简体   繁体   中英

htaccess rewrite slug to seo url

I have some URLs like this:

www.mydomain/works/show.php?slug=title-of-the-work

I'll like to have htaccess convert to seo URLs in this way:

www.mydomain/works/title-of-the-work

In my database I have created a field called slug for every work entry. The first URL works fine.

This is how my .htaccess currently looks (it is placed in my root directory):

RewriteEngine On
RewriteRule /works/(.*)$ /works/show.php?slug=$1

I've read and tried a lot of similar examples during the last 24hours to no avail. I'm sure my server allows rewriting, because I can for example rewrite non-www to www URLs. Hope someone can help. Thanks

Use:

RewriteEngine On
RewriteRule ^works/(.*)$ works/show.php?slug=$1 [L]

You can also put your .htaccess file into works folder with this code:

RewriteEngine On
RewriteRule ^(.*)$ show.php?slug=$1 [L]

Finally this is the htaccess I'm using (inside works folder) :

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^/]+)/?$ show.php?slug=$1 [L]

Thanks userNOID for putting me on the right track. There was indeed a redirection loop between htaccess and my php code, which was solved by adding the two conditions above to tell the server to ignore the redirection if the file phisically exists (true in 4 cases: /works, /works/web-design, /works/graphic-design and works/apps)

I also changed the regular expression a bit to be able to have the URLs work with and without trailing slash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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