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

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.

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

htaccess: how to restrict IP Address for URL path?

(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]

URL rewrite in html

I want to do URL rewrite in html pages.
Any help for that. every one knows about url rewrite but I found all article for pp, asp.net, classic asp. so please any one knows how to do url rewrite in html.
like wise I want to rewrite from
http://www.xyz.com/aboutus.html to http://www.xyz.com/About-us
Any help will appreciated.
Thank you.
I'm not normally huge on working with RewriteEngine, or .htacess, but according to this blog entry, you can use the following code to hide file extensions:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Just paste that in your .htaccess file, and put the changes to the server (upload the new .htaccess using an HTTP client). If your .htaccess already has RewriteEngine on, you should skip the first line.
If you want to change the URL from xyz.com/aboutus.html to xyz.com/About-Us, you also have to change the name of the file or folder aboutus to About-Us. Another possible solution would involve just having an index.html file in a folder named About-Us, which would make the server load that file automatically once a user accesses xyz.com/About-Us, and wouldn't display the filename.
To rewrite urls you need to configure the webserver to do that (for example in apache with .htaccess.
But if you want to do that without use server configuration, a bad solution exists: make a folder with the name of the url and put the html into that with the name index.php. For example about-us.html > about-us/index.html and in links put the url about-us. But is a bad solution.

MySQL + htaccess mod_rewrite?

I'm using a proxy-like short domain in conjunction with my site. The short domain is hrci.me and the long domain is reachchallenges.infectionist.com. hrci.me uses mod_rewrite and has a rule that pretty much does a simple redirect from hrci.me to reachchallenges.infectionist.com, so for example:
hrci.me/x/y.php
would redirect to
reachchallenges.infectionist.com/x/y.php
Simple as can be. On the main site I have more rules that further rewrite the URL, prettifying it. One example is a script on my site, challenges.php, which accepts a single parameter, chid, which is the challenge ID linked to more information in the database. Passed as a parameterized script it would look like this: /challenge.php?chid=123, but after it's rewritten it looks like this: /challenge/123/Challenge+Title/, where Challenge+Title is the actual title of the item from the database. There's also a different way you can call the same page, like this: /ch123, so in essence you can access the page 3 different ways:
1. /challenge.php?chid=123
2. /challenge/123/Challenge+Title/
3. /ch123
This actually works perfectly, the issue that I have is that I want the URLs that are redirected from hrci.me to first be rewritten to look like #2 above, so the user would click hrci.me/ch123 and the htaccess file would read the database, get the title for challenge id 123, rewrite the url to /challenge/123/Challenge+Title/ and then redirect it to reachchallenges.infectionist.com. Is something like this possible? Is it possible to read from a MySQL database using htaccess in this way?
UPDATE:
I added this to my httpd.conf file:
DBDriver mysql
DBDParams "host=*****,user=*****,pass=*****,dbname=*****"
RewriteMap hrci "dbd:SELECT title FROM challenges WHERE id = %s"
RewriteLog "/home/halo2freeek/rewrite.log"
RewriteLogLevel 3
Then added a RewriteRule to one of my subdomains that I don't really use (to test it):
RewriteEngine On
RewriteRule ^ch([0-9]{1,4})(/)?$ http://reachchallenges.infectionist.com/challenge/$1/${hrci:$1} [R=301,L]
When I visit this path on the subdomain:
/ch232
It should redirect to:
http://reachchallenges.infectionist.com/challenge/232/Challenge+Title
But instead it redirects to:
http://reachchallenges.infectionist.com/challenge/232/
Without the title. Am I doing something wrong? Do I have to specify RewriteEngine On in the httpd.conf file?
UPDATE 2: Ok, so I added the RewriteEngine On line and saved, when I tried to restart Apache I got this error:
RewriteMap: file for map hrci not found:/dh/apache2/apache2-ps54462/dbd:SELECT title FROM challenges WHERE id = %s
It looks like it's completely ignoring the dbd part and trying to read the whole thing as a file name. Now I really don't know what I'm doing wrong.
With RewriteMap everything is possible:
RewriteMap examplemap prg:/path/to/file.php
RewriteRule (.*) ${examplemap:$1}
You can use mod_dbd as well:
DBDriver mysql
DBDParams "host=localhost,user=db_user,pass=password,dbname=db"
RewriteMap myquery "dbd:select new_url from rewrite where old_url = %s"
RewriteRule (.*) ${myquery:$1}