简体   繁体   中英

make seo friendly url using .htaccess

I'm trying to make SEO url using .htaaccess
this is my .htaccess :

AddDefaultCharset utf-8
Options +FollowSymLinks
RewriteEngine On

RewriteRule ^ads/([0-9]+) advertisement.php?ad_id=$1 [NC,L]
RewriteRule ^rss/([0-9]+)-([^/]+) rss.php?cat=$1 [NC,L]

#replace template folder with a fake folder named theme
RewriteRule ^theme/(.*) template/$1 [NC,L]

#replace /core/ajax folder with a fake folder named ajax
RewriteRule ^a/(.*) core/ajax/$1 [NC,L]

RewriteRule ^news/([0-9]+)-([0-9]+)$ direct.php?cat=$1&post_id=$2 [NC,L]
RewriteRule ^(.*)/([0-9]+)-([^/]+)$ direct.php?cat=$1&post_id=$2 [NC,L]
RewriteRule ^(.*/)page/([0-9]+)$ category.php?cat=$1&p=$2 [NC,L]



RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ category.php?cat=$1 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php [NC,L]

i want to call php files directly without .php and if the file doesnot exist send request to category.php like you see above both are ok and work fine separately when i put them in same file they don't work.
example :
last-news.php is a existing file, and sport don't exist
i want to be like this
1. http://example.com/last-news open http://example.com/last-news.php
2. http://example.com/sport open http://example.com/category.php?cat=sport
right now number 2 works but i can't make number 1 work as well
can anyone here help me ?

solved the problem myself,code is right but placement is wrong
simply just replaced

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ category.php?cat=$1 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php [NC,L]

with :

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ category.php?cat=$1 [NC,L]

and works fine...

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