Nikhil sHETH
Nikhil sHETH

Reputation: 530

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

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]

Upvotes: 0

Views: 132

Answers (2)

Nikhil sHETH
Nikhil sHETH

Reputation: 530

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]

Upvotes: 0

anubhava
anubhava

Reputation: 785256

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]

Upvotes: 1

Related Questions