douguai7291 2012-07-15 21:27
浏览 30
已采纳

创建多个htaccess重写

I currently have a .htaccess file that rewrites dyhamb.com/episode.php?episode=1 as dyhamb.com/1. I would also like another that rewrites dyhamb.com/blogpost.php?bp=1 as dyhamb.com/blog/1.

I have the code set up for the episode rewrite already however when I go to add the blog rewrite I can't seem to get it to work. How would I alter the following to make that possible?

Options -Multiviews

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^dyhamb\.com$
RewriteRule ^(.*) http://dyhamb.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(0|[1-9]\d{0,2})$ /episode.php?episode=$1 [L,QSA]
RewriteRule ^/blog$ /blogpost.php?blog=$1 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+episode\.php\?episode=(\d+) [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+blogpost\.php\?blog=(\d+) [NC]

RewriteRule ^ %1? [R=301,L]
  • 写回答

1条回答 默认 最新

  • drzb7969753 2012-07-16 15:50
    关注

    You need to separate the 2 and duplicate the set of conditions that you have. The conditions only apply to the immediately following rule:

    RewriteCond <something>
    RewriteCond <something-else>
    # those 2 conditions only apply to this rule:
    RewriteRule <match> <target>
    
    # This rule has no conditions
    RewriteRule <match2> <target2>
    

    So you want your htaccess to look something like this:

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} !^dyhamb\.com$
    RewriteRule ^(.*) http://dyhamb.com/$1 [R=301,L]
    
    # Setup conditions for internal rewrite of episode.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Rewrite for episode.php
    RewriteRule ^(0|[1-9]\d{0,2})$ /episode.php?episode=$1 [L,QSA]
    
    # Setup conditions for internal rewrite of blopost.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Rewrite for blogpost.php
    RewriteRule ^blog/(.*)$ /blogpost.php?blog=$1 [L,QSA]
    
    # External redirect for episodes
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+episode\.php\?episode=(\d+) [NC]
    RewriteRule ^ /%1? [R=301,L]
    
    # External redirect for blog
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+blogpost\.php\?blog=(\d+) [NC]
    RewriteRule ^ /blog/%1? [R=301,L]
    

    Note that there needed to be some changes to your blog rules. If these rules are going to be in an .htaccess file, the leading slash is stripped off of the URI before the rewrite engine processes it, so the expression ^/blog needed to be ^blog, and I added a backreferenced match (.*) after the blog since you want to be able to access the ID after it to insert into the blog= query string in your target. Also, the external redirect for blog was missing the /blog/ before the ID.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面