htaccess: how to restrict IP Address for URL path? - html

(I don't think the above link provides an answer for this question; see comment below)
I'm using a CMS system that uses a database instead of the traditional file-directory structure to store/manage webpages. Since there's no folder for each webpage, I can't place an .htaccess file in specific folders to control access.
There is one .htaccess file that exists in the root directory. Is it possible to use that .htaccess file to restrict access by IP Address for a specific URL? If so, what's the syntax? Note, it must be for URL because I don't believe there is even a specific file with .html extension for a webpage with this CMS (it's all handled behind the scenes somehow).
For example, the .htaccess file is here:
/home/username/public_html/.htaccess
and the URL needing access control is here:
https://www.mycompany.com/locations/europe/contactus/
What's the code to place in the .htaccess file to only allow the IP Address of 111.222.333.444 (for example) access to the above URL?
If this isn't possible, is there another way to solve this problem for restricting IP Address to a specific webpage?

Using mod_rewrite to control access
Here is the documentation of it, you may go to section "Blocking of Robots" to block IP range which trying to access particular url.
I think you may try this, I am not sure whether is works, but still try it, hope it works for you.
RewriteEngine on # this is for opening the module
RewriteCond %{REMOTE_ADDR} =123\.45\.67\.[8-9]
RewriteRule ^/locations/europe/contactus/ - [F]

Related

Remove subfolder using URL rewriting

I'm not an expert of URL rewriting but I'd like to use .htaccess to show up a path like this:
https://www.example.com/folder/login/
in this way:
https://www.example.com/folder/
but I don't want a real redirect: my site should display https://www.example.com/folder/login/ page, but the URL would be different, so that an user could think that he still is on https://www.example.com/folder/ while logging in.
Is it possible?
If it is, one last question: I have already enabled RewriteEngine on on the ROOT folder, do I have to put it in another .htaccess file too (which is places into https://www.example.com/folder/login/)?
/folder/ is the requested URL and what appears in the browser's address bar and /folder/login/ is the underlying filesystem path that the request is rewritten to.
To internally rewrite /folder/ to /folder/login/ try the following in the root .htaccess file.
RewriteEngine On
RewriteRule ^folder/$ /folder/login/ [L]
/folder/login/ is presumably a filesystem directory, so this won't directly handle the request. Ideally, you would rewrite directly to the file that handles the request. In this case, I assume mod_dir will issue an internal subrequest for the directory index document, eg. index.php?
I have already enabled RewriteEngine on on the ROOT folder
Note that the order of directives matters.
do I have to put it in another .htaccess file too (which is places into https://www.example.com/folder/login/)
No, you can put it in the root .htaccess file. (You could put it in /folder/.htaccess, but the directives would need to change. It wouldn't make sense to put it in /folder/login/.htaccess since you then couldn't hide the /login subdirectory.)

htaccess - change part of requested URL before sending user onwards?

I have the following issue: we have an internal documentation system which generates help files as HTML. The software that generates them was recently upgraded and now there is a naming issue between capitalized and non capitalized folders.
Old URLs:
https://documentation.example.com/de/101/Skins/Default/Stylesheets/Components/Tablet.css
New URLs:
https://documentation.example.com/de/101/skins/default/stylesheets/components/tablet.css
We have different folders that are affected, so the URLs could look like this (there are many variations):
https://documentation.example.com/en/103/Skins/Default/Stylesheets/Components/Tablet.css
https://documentation.example.com/de/456/Skins/Default/Stylesheets/Components/Tablet.css
https://documentation.example.com/en/324/Skins/Default/Stylesheets/Components/Tablet.css
I would like to be able to take the requested URL, look for this string: "/Skins/Default/Stylesheets/Components/Tablet.css" change it to lowercase and then send the user on to the new changed URL.
Some of the solutions I have found require access to vhosts files which I don't. I am also unable to upload a PHP file or something like that.
Are there any solutions to my problem that could be done with the htaccess file alone? If yes how?
You need to define a rewrite map in your server/vhost config file where the css is hosted.
RewriteMap lc int:tolower
Then in your htaccess file, you can create a rule like:
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(.*)$ ${lc:$1} [L,NC]
Let me know if you need anymore help. This htaccess rule will cause every url to be converted to lowercase. This is a generic htaccess you could make it more refined to search specifically for the URL you need.

htaccess smart url for listing directories

1> This is the htaccess rule to display a smart URL for the path /uploadr/public folder !!
/uploadr/public folder is the one to list the directories and does not contain any html documents or such
RewriteRule ^Uploadr-Public$ /uploadr/public [L]
However when i access http://localhost/Uploadr-Public in the url the path
mapping works fine but does not display the smart URL (Uploadr-Public)
How can is solve this ?
Heres the screenshot of what it is currently displaying in url!!
2> Also if suppose one wants to access a folder named temp which is in
/var/www/html/uploadr/public/temp
can he specify the path in url as
htttp://localhost/Uploadr-Public/temp
where Uploadr-Public/ is smart URL and temp is a folder inside public
Issue is that /uploadr/public points to a physical directory and you're not using trailing slash in rewritten path. mod_dir module that runs after mod_rewrite adds a trailing slash in front of directories.
You can use this rule to fix it:
RewriteRule ^Uploadr-Public/(.*)$ /uploadr/public/$1 [L,NC]
Make sure to clear browser cache before testing it.

Should a subdomain be accessible as subfolder?

I have a subdomain for my website created in cPanel and I've noticed that in addition to being able to access the content through this URL:
subdomain.example.com
It can also be accessed via:
example.com/subdomain
Questions:
Is that normal?
Is there any way to only allow access to the
subdomain through, well, the subdomain?
There's nothing wrong with it as long as either you don't send users to the directory, or the applications and pages in that directory can handle using two different URLs (e.g. it uses only relative URLs).
If you want to block the directory, then try this htaccess directive:
RewriteRule ^subdomain/ - [L,R=404]

Temporary way to convert base urls in HTML

Right now I have a website with all the href and src point to /some/url. There's always a slash in the beginning.
I'm currently testing the site using a subdirectory on the server, which has the address of http://somedomain.com/subdirectory/ (production would be http://somedomain.com)
Is there anyway to temporarily convert the /some/url from http://somedomain.com/some/url to http://somedomain.com/subdirectory/some/url without actually changing each of the href and src properties. From what I can make out of the base tag, it doesn't work like that. Also changing the RewriteBase from / to /subdirectory also doesn't work (Not too familiar with mod_rewrite, maybe someone can fill me in on that)
in .htaccess file in somedomain.com dir U can put:
Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*) http://somedomain.com/subdirectory/$1 [r=301,nc]
I haven't tried it, but mod_proxy_html might be able to do this. From the documentation:
mod_proxy_html is an output filter to
rewrite HTML links in a proxy
situation, to ensure that links work
for users outside the proxy. It serves
the same purpose as Apache's
ProxyPassReverse directive does for
HTTP headers, and is an essential
component of a reverse proxy.
For example, if a company has an
application server at
appserver.example.com that is only
visible from within the company's
internal network, and a public
webserver www.example.com, they may
wish to provide a gateway to the
application server at
http://www.example.com/appserver/.
When the application server links to
itself, those links need to be
rewritten to work through the gateway.
mod_proxy_html serves to rewrite foobar
to foobar
making it accessible from outside.