.htaccess帮助文件夹?

时间:2013-04-15 05:44:03

标签: php .htaccess

我正在尝试用htaccess创建漂亮的网址.. 我希望从www.mysite.com/movie/name.html

创建网址www.mysite.com/movie/movie.php?url=name

这里电影/是一个文件夹,同样我还有两个文件夹,比如专辑/和新闻/,我想让网址像下面那样

  • www.mysite.com/albums/name.html来自ww.mysite.com/albums/album.php?id=name

  • www.mysite.com/news/name.html来自ww.mysite.com/news/album.php?id=name

.htaccess位于

之下
<IfModule mod_rewrite.c>
# removing .php extension 
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php
#Code to rewrite www.mysite.com/movie/name to www.mysite.com/movie/movie.php?url=name
RewriteEngine On
RewriteRule ^(.+)/([a-zA-Z0-9_-]+)$ movie.php?url=$1
</IfModule>

但我正在重定向每个页面

请建议正确的代码。

2 个答案:

答案 0 :(得分:1)

重定向php文件使用:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/])/$ /$1.php [L]

然后是特定页面:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/])/([0-9]+)/([^/])\.html$ /$1.php?url=$2 [L,QSA]

您可以查看文档 here

答案 1 :(得分:1)

试试这个

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^ - [S=3,L]

#www.mysite.com/movie/movie.php?url=name

RewriteRule ^movie/([^/]+)\.html?(.*)$ movie/movie.php?url=$1$2 [QSA,S=2,L]

#www.mysite.com/news/album.php?id=name

RewriteRule ^news/([^/]+)\.html?(.*)$ news/album.php?id=$1$2 [QSA,S=1,L]

#www.mysite.com/albums/album.php?id=name

RewriteRule ^([^/]+)/([^/]+)\.html?(.*)$ $1/$2.php?id=$2$3 [QSA,L]