简体   繁体   中英

SEO friendly URL not working using .htaccess

I'm trying to implement a SEO friendly URL using .htaccess by using the RewriteRule below

RewriteRule ^n/article/([a-zA-Z0-9]+)/$ article.php?title=$1

The actaul URL looks like this

http://localhost/n/article.php?title=this-is-the-first-news-article

but I want it to look like this :

http://localohst/n/article/this-is-the-first-news-article

When I applied the RewiteRule above it does not change to the desired URL

This should do it. You are missing the n. Not sure why you need the word title though.

RewriteRule ^n/article/title/([a-zA-Z0-9]+)/$ article.php?title=$1

You have to capture the query string first. You can't do that with a RewriteRule because they ignore the query string. Here we're using [R] to redirect. If this is working for you and there is the potential that the old URLs are being stored somewhere as links, then you may want to specify [R=301] . Be sure to remove all old-style links from your site though (that contain the previous link format we're rewriting), that way you're not penalized for not updating your links.

RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} title=([^&]+)
RewriteRule ^n/article.php n/article/%1? [R,L]

If your site needs the formatting to function from the original, you might also need this after the first rule. This rule quietly redirects the URL back to the original without showing it to the end user:

RewriteRule ^n/article/(.*) n/article.php?title=$1 [L]

它将是RewriteRule ^ n / article / title /([[a-zA-Z0-9] +)/ $ article.php?title = $ 1

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