当网址包含空格,数字或短划线时,如何使用htaccess外部重定向网址

时间:2016-05-06 07:36:21

标签: .htaccess redirect external space

我是htacces的新手。

我想创建像stackoverflow一样的htaccess。

检查任何stackoverflow的网址,如" hide file extension in url by htaccess"。如果您将.html / .php / .asp / .abc / .xyz放入任何内容,它将重定向到" hide file extension in url by htaccess"只有你把/最后它没有效果

表示我想说url filename包含任何键盘字符,它将在外部重定向。

下面是我目前的htaccess

RewriteEngine on

# To internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.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]

2 个答案:

答案 0 :(得分:1)

你需要使用适当的``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 ^(.+?)/?$ $1.html [L]

答案 1 :(得分:0)

在@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 ^(.+?)/?$ $1.html [L]