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

Php 显示不带扩展名的正确URL&;如果URL不正确,则重定向,php,regex,.htaccess,redirect,url-rewriting,Php,Regex,.htaccess,Redirect,Url Rewriting,我在这个问题上加了50英镑的赏金。我正在努力做到: 所有网站页面均显示www.example.com/page/-而不是/page。我下面的当前代码仅适用于www.example.com/page 如何更正.htaccess代码以实现这一点 另外,将规范的URL元标记从www.mysite.com/page更改为www.example.com/page/——搜索引擎中将显示www.example.com/page/,而不是/page 当前的.htaccess代码是: RewriteEngine

我在这个问题上加了50英镑的赏金。我正在努力做到:

  • 所有网站页面均显示www.example.com/page/-而不是/page。我下面的当前代码仅适用于www.example.com/page
  • 如何更正.htaccess代码以实现这一点

    另外,将规范的URL元标记从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
    
    
    重新启动发动机
    重写库/博客/
    重写规则^index\.php$-[L]
    重写cond%{REQUEST_FILENAME}-F
    重写cond%{REQUEST_FILENAME}-D
    重写规则/blog/index.php[L]
    #开始WordPress
    重新启动发动机
    重写库/博客/
    重写规则^index\.php$-[L]
    重写cond%{REQUEST_FILENAME}-F
    重写cond%{REQUEST_FILENAME}-D
    重写规则/blog/index.php[L]
    #结束WordPress
    
    我不确定这是如何工作的,但我很想了解更多关于.htaccess和重定向的信息,但是对于初学者来说,网络上似乎没有什么东西。我也读过这篇文章。它也必须是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
    
    
    重新启动发动机
    重写库/博客/
    重写规则^index\.php$-[L]
    重写cond%{REQUEST_FILENAME}-F
    重写cond%{REQUEST_FILENAME}-D
    重写规则/blog/index.php[L]
    #开始WordPress
    重新启动发动机
    重写库/博客/
    重写规则^index\.php$-[L]
    重写cond%{REQUEST_FILENAME}-F
    重写cond%{REQUEST_FILENAME}-D
    重写规则/blog/index.php[L]
    #结束WordPress
    
    这对我很有效,顶部的一个从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]
    

    将您的.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/图像的相对路径问题,请在页面HTML的
    部分下方添加以下内容:

    <base href="/" />
    
    
    
    请编辑您的答案,并在代码中添加一些解释。不鼓励只使用代码的答案,可能会被删除。谢谢,但这对我不起作用。它的功能与我的代码相同。它不显示example.com/page/,而是显示example.com/page-它也不会从.html重定向到/或从一页重定向到另一页/这几乎解决了问题,谢谢,但现在css样式表和js脚本不会加载,因为当我在inspector中单击它们时,它们显示.html.html.html.html.html.html.html.html.html.html.html的次数太多了。有没有办法解决这个问题?我应该提到的另一点是,目前页面都是.html,但我希望它们显示为/而不带.html扩展名,因为我可能还会将页面更改为.php-(扩展名是否为/,这并不重要)请查看我的更新答案,以及关于
    标签的说明。是的,您需要在所有页面中添加
    标签,以使其正常工作,否则将无法加载css/js等(由于您在整个网站中使用了相关链接)。让我们一起来看看。