重新运行或继续规则

时间:2014-11-20 14:06:28

标签: regex apache .htaccess mod-rewrite redirect

我有一些重写会将文件名更改为url结构。

http://example.com/landing-myfile.html将转换为http://example.com/landing/myfile/

我正在尝试设置默认页面,保留网址,然后重新处理规则。

因此,如果用户输入http://example.com/asdfasdfasdf

它会重定向到http://example.com/landing-mydefault.html

但我希望它显示为http://example.com/landing/mydefault/

我有重写适用于所有漂亮的URL,我丢失了重定向到默认页面的文件夹/文件。但是我需要继续重写所有其他规则。

这是我到目前为止所拥有的。

RewriteEngine On
RewriteBase /

#redirects bad requests to a default.
#works but leavings landing-mydefault.html in the address bar.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /landing-mydefault.html [R,L]

#process the pages and converts them to pretty urls.
#everything below works as it should.
RewriteCond %{THE_REQUEST} landing-([^.]+)\.html\s [NC]
RewriteRule ^ /landing/%1/ [R=302,L,NE]
RewriteCond %{THE_REQUEST} landing-([^.]+)\.html\?v=([^\s&]+) [NC]
RewriteRule ^ /landing/%1/%2? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^landing/([^/]+)/?$ /landing-$1.html [L]
RewriteRule ^landing/([^/]+)/([^/]+)/?$ /landing-$1.html?v=$2 [L,QSA]

1 个答案:

答案 0 :(得分:2)

按此顺序尝试规则:

RewriteEngine On
RewriteBase /

RewriteRule ^/?$ /landing/mydefault/ [R,L]

#redirects bad requests to a default.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^landing/ /landing/mydefault/ [R,L,NC]

#process the pages and converts them to pretty urls.
#everything below works as it should.
RewriteCond %{THE_REQUEST} \s/+landing-([^.]+)\.html\?v=([^\s&]+) [NC]
RewriteRule ^ /landing/%1/%2? [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s/+landing-([^.]+)\.html\s [NC]
RewriteRule ^ /landing/%1/ [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^landing/([^/]+)/?$ landing-$1.html [L,NC]

RewriteRule ^landing/([^/]+)/([^/]+)/?$ landing-$1.html?v=$2 [L,NC]