【问题标题】:Rerun or continue rules重新运行或继续规则
【发布时间】:2015-01-18 10:19:14
【问题描述】:

我进行了一些重写,会将文件名更改为 url 结构。

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

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

所以如果用户输入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]

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite redirect


    【解决方案1】:

    按此顺序尝试规则:

    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]
    

    【讨论】:

    • 似乎无论在地址栏中放什么,即使是有效文件,都会返回一个网页不可用的错误。
    • 你是帮助我编写漂亮网址的人。 Here's the link如果它有助于解决这个问题。谢谢。
    • 它将保留地址栏中的任何内容。
    • 好的 - 很明显我的网络服务器已经退出 - 这就是我让网页不可用的原因 - 但有些由于缓存而工作。我尝试了您的原始更新-仍然无效。上面列出的更新(当前)返回一个重定向循环。此外,即使它存在,landing-a.html 和landing-b.html 也会重定向到landing/mydefault/,并带有重定向循环。
    • 这似乎适用于除根级别之外的所有内容 - 这只是一个文件列表。在这种情况下没有索引页。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-25
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 2022-08-06
    相关资源
    最近更新 更多