简体   繁体   中英

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.

Example: 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.

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.

How can I write that to a rule in .htaccess ?

Sounds pretty straight forward: you check the http host, the presence of the pass and react as desired:

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):

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. Typically such rules should get implemented in the actual http server's host configuration. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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