当 url 包含 space、数字或破折号时,如何使用 htaccess 从外部重定向 url

How externally redirect url using htaccess, when url contain space, number or dash

我是 htacces 的新手。

我想像 Whosebug 一样创建 htaccess。

检查 Whosebug 的任何 url,如“hide file extension in url by htaccess". If you put .html/.php/.asp/.abc/.xyz anything it will redirect to "hide file extension in url by htaccess”,即使你输入 / 最后它也没有效果

意味着我想说 url 文件名包含任何键盘字符,它将在外部重定向。

下面是我当前的 htaccess

RewriteEngine on

# To internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ .html


# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]

您需要使用合适的``RewriteBase`:

RewriteEngine on
RewriteBase /folder1/folder2/

# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} ^GET\s(.+?)\.html[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]

# To internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+?)/?$ .html [L]

在@anubhava 的帮助下我最终的 htaccess 文件

RewriteEngine on
RewriteBase /folder1/folder2/

# To externally redirect /folder1/folder2/file.html to /folder1/folder2/file.html or any extension like .php/.asp/.abcd etc
RewriteCond %{THE_REQUEST} ^GET\s(.+?)\.[a-z0-9]{2,}[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]

# To internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+?)/?$ .html [L]