简体   繁体   English

如果请求 URL 在 .htacess 中没有字符串,如何阻止访问

[英]How prevent access if the request URL doesn't have string in .htacess

I have an alias domain name which I don't want it to load the website.我有一个别名域名,我不希望它加载网站。 I only want my alias domain to access if the request URL has a certain string.如果请求 URL 具有特定字符串,我只希望我的别名域能够访问。

Example: www.myalias.com/pass/whatever/here示例: www.myalias.com/pass/whatever/here

The string I'm looking at is the "pass" If the request URL has the "pass" there, then allow to access to the website, if not then return 404 error.我正在查看的字符串是“pass” 如果请求 URL 在那里有“pass”,则允许访问该网站,否则返回 404 错误。

The rule will check if the domain name is equal to "myalias.com" then check to see if the string "pass" is exist in the request URL.该规则将检查域名是否等于“myalias.com”,然后检查请求 URL 中是否存在字符串“pass”。

How can I write that to a rule in .htaccess ?如何将其写入 .htaccess 中的规则?

Sounds pretty straight forward: you check the http host, the presence of the pass and react as desired:听起来很简单:您检查 http 主机、pass 的存在并根据需要做出反应:

This denies requests这拒绝请求

RewriteEngine on
RewriteCond %{HTTP_HOST} ^alias\.example\.com$
RewriteCond %{REQUEST_URI} !^/pass/
RewriteRule ^ - [F]

That one sends a custom http status (here 403):那个发送一个自定义的 http 状态(这里是 403):

RewriteEngine on
RewriteCond %{HTTP_HOST} ^alias\.example\.com$
RewriteCond %{REQUEST_URI} !^/pass/
RewriteRule ^ - [R=403]

Or you can redirect clients to wherever you want:或者您可以将客户重定向到您想要的任何地方:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^alias\.example\.com$
RewriteCond %{REQUEST_URI} !^/pass/
RewriteRule ^ https://example.com/ [R=301]

You obviously need to have the rewriting module loaded inside your http server.您显然需要在 http 服务器中加载重写模块。 Typically such rules should get implemented in the actual http server's host configuration.通常,此类规则应在实际 http 服务器的主机配置中实现。 If you really want to use a distributed configuration file (".htaccess") you need to enable its interpretation first (see the documentation for the AllowOverride directive).如果您真的想使用分布式配置文件(“.htaccess”),您需要先启用它的解释(请参阅AllowOverride指令的文档)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM