使用重写规则简化 URL
Simplifying the URL using ReWrite Rule
我有一个博客,在 categories.php
页面中有多个类别。而原来的 URL 看起来是这样的:
http://myname.com/articles.php?category=music
我已经简化为:
http://myname.com/articles/music
使用:
RewriteRule ^articles/([\w-]+)/?$ articles.php?cat= [NC,L,QSA]
但是有没有办法将其进一步简化为:
http://myname.com/music
--
更新:
我已经解决了原来的问题,但现在遇到了另一个问题。我的 .htaccess 是:
Options -MultiViews
DirectoryIndex articles.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ articles.php?cat= [NC,L,QSA]
RewriteRule ^([\w-]+)/([0-9]+)/?$ articles.php?cat=¤tpage= [NC,L,QSA]
RewriteRule ^([0-9]+)/([\w-]+)/?$ article.php?id=&title= [NC,L,QSA]
RewriteRule ^articles articles.php
RewriteRule ^categories categories.php
RewriteRule ^about about.php
RewriteRule ^feed feed.php
RewriteRule ^privacy privacy.php
当我访问 http://myname.com/articles
和 http://myname.com/article...
时一切正常,但是当我转到 http://myname.com/categories
、http://myname.com/feed
等时,我被重定向回主页.知道这是为什么吗?
您可以在 DOCUMENT_ROOT/.htaccess
文件中使用此规则:
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ articles.php?cat= [L,QSA]
我有一个博客,在 categories.php
页面中有多个类别。而原来的 URL 看起来是这样的:
http://myname.com/articles.php?category=music
我已经简化为:
http://myname.com/articles/music
使用:
RewriteRule ^articles/([\w-]+)/?$ articles.php?cat= [NC,L,QSA]
但是有没有办法将其进一步简化为:
http://myname.com/music
--
更新:
我已经解决了原来的问题,但现在遇到了另一个问题。我的 .htaccess 是:
Options -MultiViews
DirectoryIndex articles.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ articles.php?cat= [NC,L,QSA]
RewriteRule ^([\w-]+)/([0-9]+)/?$ articles.php?cat=¤tpage= [NC,L,QSA]
RewriteRule ^([0-9]+)/([\w-]+)/?$ article.php?id=&title= [NC,L,QSA]
RewriteRule ^articles articles.php
RewriteRule ^categories categories.php
RewriteRule ^about about.php
RewriteRule ^feed feed.php
RewriteRule ^privacy privacy.php
当我访问 http://myname.com/articles
和 http://myname.com/article...
时一切正常,但是当我转到 http://myname.com/categories
、http://myname.com/feed
等时,我被重定向回主页.知道这是为什么吗?
您可以在 DOCUMENT_ROOT/.htaccess
文件中使用此规则:
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ articles.php?cat= [L,QSA]