代码之家  ›  专栏  ›  技术社区  ›  user8758206

显示正确的URL,不带扩展名;如果URL不正确,则重定向

  •  3
  • user8758206  · 技术社区  · 7 年前

    我添加了一个 悬赏50英镑 对这个问题。我正努力做到:

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

    怎么可能。要实现这一点,需要更正htaccess代码吗?

    另外,从www.mysite更改规范URL元标记。com/page至www.example。com/page/-will www.example。在搜索引擎中显示com/page/show而不是/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和重定向,但对于初学者来说,web上似乎没有什么内容。我也读过 this 邮递它也必须是https(不知道为什么,但博客帖子也可以有http链接,而其他页面重定向到https)。

    该网站上还有一个博客(php文件),上面有自己的htaccess文件,安装在示例中。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 回复  |  直到 4 年前
        1
  •  2
  •   anubhava    7 年前

    有你的。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> 页面HTML的部分:

    <base href="/" />
    
        2
  •  0
  •   xCoded    7 年前

    这对我来说很有效,最重要的是排除。php从URL中删除,最下面的排除。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]