简体   繁体   中英

url Slug php and htaccess issue

I'm trying to create a slug to jump from home page to article page with the title as a slug.

here are my .htaccess codes:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^post/([a-zA-Z0-9_]-)/([0-9]+)\$ article.php?id_art=$1 [NC,L]

and here is my php link:

<a href='article/post/$id_art/$slug'><button class='read-more-btn'>Read more</button></a>

The slug in url appears pefectly like this:

http://localhost/sitename/article/post/8/the-slug-i-want-to-appear

but I got this result:

Object not found!
Error 404

what's the problem guys ? I guess it's the .htaccess. help please

You have your regex backwards. You have post then ID then slug , but your regex matches post then slug (numbers/letters/underscores) then ID . Try:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^post/([0-9]+)/([a-zA-Z0-9_-]+)/?$ article.php?id_art=$1 [NC,L]

Note that you should have been getting an error for the expression: [a-zA-Z0-9_]- , which isn't valid.

It could be because you have a rule that matches ^post/ ( but you don't have a rule for article, try RewriteRule ^article/post/([a-zA-Z0-9_]-)/([0-9]+)\\$ article.php?id_art=$1 [NC,L]` and see if that helps.

You also have sitename which isn't in the rule. What is your original url you're trying to rewrite with the article.php in the link? Try to include that also.

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