dqrzot2791
2014-06-30 12:09 阅读 70
已采纳

.htaccess用于多个重写规则

I wanted to create an htaccess file to remove the .php extension and change course.php?id=1 to course/1. But that does not seem to work out. Can any one correct my htaccess?(I am quite new to writing htaccess files)

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

# If request is for a file that does actually exist, don't rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
 # If request is for a directory that does actually exist, don't rewrite.
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^/course/(.*)$ /course.php?id=$1 [NC]

Options -Indexes
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    duanfang7270 duanfang7270 2014-06-30 12:15

    Keep it this way:

    Options -Indexes
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    
    # add a trailing slash
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule ^(.+)$ $1/ [R=301,L]
    
    RewriteRule ^course/([^/]+)/?$ course.php?id=$1 [NC,L,QSA]
    
    RewriteRule ^([^/]+)/$ $1.php [L]
    RewriteRule ^([^/]+)/([^/]+)/$ $1/$2.php [L]
    
    点赞 评论 复制链接分享

相关推荐