当网址包含空格,数字或破折号时,如何使用htaccess从外部重定向网址 [英] How externally redirect url using htaccess, when url contain space, number or dash

查看:96
本文介绍了当网址包含空格,数字或破折号时,如何使用htaccess从外部重定向网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是htacces的新手。

I am new in htacces.

我想创建类似stackoverflow的htaccess。

I want to create htaccess like stackoverflow.

检查stackoverflow的所有网址,例如 通过htaccess隐藏网址中的文件扩展名。如果您将.html / .php / .asp / .abc / .xyz放入任何内容,它将重定向到 通过htaccess隐藏url中的文件扩展名,即使您放了/最后也没有任何作用

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

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

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

以下是我当前的htaccess

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]


推荐答案

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

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]

这篇关于当网址包含空格,数字或破折号时,如何使用htaccess从外部重定向网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆