使用.htaccess制作seo友好的网址

时间:2015-10-31 16:05:48

标签: php apache .htaccess mod-rewrite

我正在尝试使用.htaac​​cess制作SEO网址 这是我的.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]

我想直接调用php文件而不使用.php,如果文件不存在,请发送请求到category.php,就像你上面看到的那样两个都可以,并且当我把它们放在同一个文件中时它们不能正常工作。<登记/> 例如:
last-news.php是现有文件,运动不存在 我想要像这样 1. http://example.com/last-news打开http://example.com/last-news.php
2. http://example.com/sport打开http://example.com/category.php?cat=sport
现在2号作品,但我不能使1号作品 这里有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我自己解决了问题,代码是对的,但放置错误 只是简单地替换了

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]

并且工作正常......