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

旧时模样 提交于 2019-12-08 09:40:02

问题


I am new in htacces.

I want to create htaccess like stackoverflow.

Check any url of stackoverflow like "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" only even you put / at last it has no effect

Means I want to say url filename contain any keyboard character and it will redirect externally.

below are my current 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]

回答1:


You need to use appropriate ``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]



回答2:


My final htaccess file with the help of great @anubhava

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]


来源:https://stackoverflow.com/questions/37066877/how-externally-redirect-url-using-htaccess-when-url-contain-space-number-or-da

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!