【问题标题】:Show correct URL without extension & redirect if URL is incorrect如果 URL 不正确,则显示不带扩展名的正确 URL 和重定向
【发布时间】:2020-09-13 17:37:54
【问题描述】:

我在这个问题中添加了 50 的赏金。我正在努力做到这一点:

  1. 所有网站页面都显示 www.example.com/page/ - 而不是 /page。我下面的当前代码仅适用于 www.example.com/page

如何更正 .htaccess 代码以实现此目的?

此外,将规范 URL 元标记从 www.mysite.com/page 更改为 www.example.com/page/ - 将 www.example.com/page/ 显示在搜索引擎中而不是 /page ?p>

当前的.htaccess 代码是:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^ example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

注意第一个重写条件只是转发到www。和 https,所以唯一可能需要编辑的部分可能是:

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

所以我想做的只是添加一个'/'。在同一个网站(位于 www.example.com/blog/)上实现我想要的 wordpress 博客(可能是 PHP 文件而不是 HTML)的 htaccess 代码为:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress

我不确定这是如何工作的,但我很想了解更多关于 .htaccess 和重定向的信息,但对于初学者来说,网络上似乎没有什么内容。我也读过this 的帖子。它也必须是 https(不知道为什么,但博客文章也可以有一个 http 链接,而其他页面重定向到 https)。

该网站上还有一个博客(php 文件),上面有自己的 htaccess 文件,安装在 example.com/blog/ 下 - htaccess 代码是默认的 wordpress 代码:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# END WordPress

【问题讨论】:

    标签: php regex .htaccess redirect url-rewriting


    【解决方案1】:

    在站点根目录中保存您的 .htaccess:

    RewriteEngine On
    
    # force www
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    # force https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    # remove .php or .html extensions
    RewriteCond %{THE_REQUEST} \s/+(.+?)\.(?:html|php)[\s?] [NC]
    RewriteRule ^ /%1 [R=301,NE,L]
    
    # add trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
    
    # add .html extension
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.+?)/?$ $1.html [L]
    
    # add .html extension
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]
    

    对于 css/js/images 的相对路径问题,请将其添加到页面 HTML 的 &lt;head&gt; 部分下方:

    <base href="/" />
    

    【讨论】:

    • 这几乎解决了问题,谢谢,但现在 css 样式表和 js 脚本不会加载,因为当我在检查器中单击它们时,它们显示 .html .html .html .html .html .html .html .html 太多次。有没有办法解决这个问题?
    • 我可能应该提到的其他一点是当前页面都是 .html,但我希望它们显示为 / 不带 .html 扩展名,因为我也可能将页面更改为 .php -(和扩展名是否只是 / )
    • 检查我更新的答案以及&lt;base ..&gt;标签上的注释。
    • 是的,您需要在所有页面中添加 &lt;base ..&gt; 标记以使其正常工作,否则 css/js 等将无法加载(由于您在网站上使用了相对链接)。
    【解决方案2】:

    这对我有用,顶部是从 URL 中排除 .php,底部是从 URL 中排除 .html。

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php [NC,L]
    
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.*)$ $1.html [NC,L]
    

    【讨论】:

    • 请编辑您的答案并为您的代码添加一些解释。不鼓励仅使用代码的答案,可能会被删除。
    • 谢谢,但它对我不起作用。它只和我的代码一样。它不显示 example.com/page/,而是显示 example.com/page - 它也不从 .html 重定向到/或从页面重定向到页面/
    猜你喜欢
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 2017-04-29
    • 2011-05-19
    • 2016-06-12
    • 2017-11-24
    相关资源
    最近更新 更多