htaccess RewriteRule问题 - 我的语法有问题吗?

时间:2014-01-02 01:36:15

标签: apache .htaccess mod-rewrite

我希望我的网站按照此协议重定向:

/weddings/ >> ?q=weddings
/editorial/one/ >> ?q=editorial&z=one

所以我做了以下重写规则:     RewriteEngine on     RewriteRule ^([^ /。] +)/ $? /?z = $ 1& q = $ 2 [nocase]

实施时完全搞砸了一切。

我做错了什么简单的事情??

我现在的.htaccess

DirectoryIndex index.html.var index.htm index.html index.shtml index.xhtml index.wml          index.perl index.pl index.plx index.ppl index.cgi index.jsp index.js index.jp index.php4 index.php3 index.php index.phtml default.htm default.html home.htm index.php5 Default.html Default.htm home.html

RewriteEngine on
RewriteCond %{HTTP_HOST} ^elijahhoffman\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.elijahhoffman\.com$
RewriteRule ^journal$ "http\:\/\/elijahj\.tumblr\.com\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^elijahhoffman\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.elijahhoffman\.com$
RewriteRule ^blog\/?$ "http\:\/\/elijahj\.tumblr\.com\/" [R=301,L]

1 个答案:

答案 0 :(得分:1)

为防止重写循环,您需要防止重写的实际文件或目录(例如索引)。附加[L]标志以在第一场比赛中停止。

RewriteEngine On

# First do your journal/blog rewrites
# Only need one condition to match both domains
RewriteCond %{HTTP_HOST} ^(www\.)?elijahhoffman\.com$
# Combine these into one rule (dots & slashes don't need escaping on the rewrite side)
RewriteRule ^(journal|blog)/?$ http://elijahj.tumblr.com/ [R=301,L]

# Then the generic one...
# If it isn't a real file or directory...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite those with only one component up to /
RewriteRule ^([^/.]+)/?$ index.php?q=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite those with 2 components
RewriteRule ^([^/]+)/([^./]+)/?$ index.php?q=$1&z=$2 [L]