简体   繁体   中英

Creating multiple htaccess rewrites

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]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM