【问题标题】:How to redirect non extension to extension page如何将非扩展重定向到扩展页面
【发布时间】:2015-05-25 16:26:49
【问题描述】:

我的网站是 php。当我打开没有任何扩展名的 url 时,我想重定向到它的原始扩展页面。 例如:当用户输入以下网址时

www.abc.com/contact

我想重定向到原始页面

www.abc.com/contact.html

在 htaccess 中我试过这个

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

但这不是重定向。它只是删除 html。

所以问题是:当用户不输入文件扩展名时,如何重定向到 .html 页面?

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    试试下面的 .htaccess 代码:

    Redirect 301 /contact www.abc.com/contact.html
    

    对于多个文件,只需尝试以下代码:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !\.[a-zA-Z0-9]{3,4}
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule ^(.*)$ $1.html
    

    【讨论】:

    • htaccess中有动态编码吗?
    • 没有得到“动态”代码?我们必须按照规则来实现它
    【解决方案2】:
    Options +FollowSymLinks -MultiViews
    
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.+)$ $1.html [L,QSA]
    

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    ## hide .html extension
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html[NC]
    RewriteRule ^ %1 [R,L,NC]
    
    ## To internally redirect /dir/foo to /dir/foo.html
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^ %{REQUEST_URI}.html[L]
    

    【讨论】:

      猜你喜欢
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      • 2014-11-04
      • 2010-12-11
      • 1970-01-01
      • 2014-03-06
      相关资源
      最近更新 更多