显示没有扩展名的正确网址&如果URL不正确,则重定向

时间:2018-01-25 00:08:28

标签: php html .htaccess redirect url-rewriting

我在此问题中添加了 50%的赏金。我试图这样做:

  1. 所有网站页面都显示www.example.com/page/ - 而不是/ page。我目前的代码仅适用于www.example.com/page
  2. 如何更正.htaccess代码以实现此目的?

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

    目前的.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]
    

    所以我只想添加一个' /'。一个wordpress博客(可能是PHP文件而不是HTML)可以在同一个网站上实现我的需求(它在www.example.com/blog/上)有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
    

2 个答案:

答案 0 :(得分: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的相对路径问题,请在页面的<head>部分下方添加:

<base href="/" />

答案 1 :(得分:0)

这适用于我,最重要的是从网址中排除.php,最下面的一个从网址中排除.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]
相关问题