如何清除空格(%20)并删除带有.htaccess的Web URL中的文件扩展名(.htm)

zpgglvta  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(112)

我正在尝试删除空格,生成%20和.htm扩展名在我的网页网址(链接)与.htaccess
我不知道如何写规则的空间,但下面是我有什么文件扩展名,这是不工作的要么。我与GoDaddy托管

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

我尝试了上面的.htaccess规则,但它不起作用。
我想将mysite.com/blog/what-is-bubul.html更改为mysite.com/blog/what-is-bubul
同时清除链接中显示为%20的空格。

gpfsuwkq

gpfsuwkq1#

无扩展请求加载文件

你得换个说法。
当您访问链接mysite.com/blog/what-is-bubul时,它应该返回文件mysite.com/blog/what-is-bubul.html

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

发现空格重定向请求(替换为-

我还添加了空间删除代码。在本例中,.htaccess文件将用户从mysite.com/what%20is%20ubul链接重定向到mysite.com/what-is-ubul链接。

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^%20]*)%20([^\s]*) [NC]
RewriteRule ^/%1-%2 [L,NE,R=301]

带注解代码

# Run this code IF "mod_rewrite" module was enabled
<IfModule mod_rewrite.c>
    # Enable server-side redirects
    RewriteEngine On

    # 1.) Remove spaces
    # 
    # %{THE_REQUEST}: url
    # ^[A-Z]{3,}\s/+([^%20]*)%20([^\s]*): find charachters before and after spaces
    # [NC]: treats lowercase and uppercase letters equally
    # 
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^%20]*)%20([^\s]*) [NC]
    #
    # if find spaces in url, then redirect url without spaces
    # 
    # ^: insert it after domain
    # %1: before chars
    # %2: after chars
    # [L]: this will last rule, instructs the server to stop rewriting
    # [NE]: without escape, instructs the server to parse output without escaping characters
    # [R=301]: redirect an entire site via 301 permanent server response
    #
    RewriteRule ^/%1-%2 [L,NE,R=301]

    # 2.) Return content of html file
    # If exists file as REQUEST_FILENAME .html in server
    #
    # %{REQUEST_FILENAME}: part after domain (mydomain.com/test -> test)
    # %{REQUEST_FILENAME}.html (mydomain.com/test -> test and .html -> test.html)
    # -f: check file is exists
    #
    RewriteCond %{REQUEST_FILENAME}.html -f
    #
    # if true, will get content of this html
    # ex.: call mydomain.com/test and exists test.html, will get content of test.html
    #
    # [NC]: treats lowercase and uppercase letters equally
    # [L]: this will last rule, instructs the server to stop rewriting after the preceding directive is processed
    #
    RewriteRule ^(.+)$ $1.html [NC,L]
</IfModule>

代码

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Remove spaces
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^%20]*)%20([^\s]*) [NC]
    RewriteRule ^/%1-%2 [L,NE,R=301]

    # Return content of html file
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.+)$ $1.html [NC,L]
</IfModule>

更多说明

.htaccess的位置

把你的.htaccess文件放在你的网站的文件夹里(和index.php/index.html等放在一起)!你不需要做任何事情来运行它。你只需要把它放在正确的地方,在web服务器调用的文件旁边!
一切都很好,但是...
如果你已经完成了这些步骤,但它仍然不起作用。
服务器不支持mod_rewrite模块:必须在服务器上启用mod_rewrite模块,重写规则才能工作。如果未启用,则重写规则将不会执行。请检查服务器设置或询问您的主机提供商是否支持此模块。
请与服务器管理员联系并请求技术帮助!

相关问题