using .htaccess to append .html behind url path - html

People are coming into my website like this:
sitename.com/brands/brandA
But this will cause an 404 error because I actually need them to come to this url
sitename.com/brands/brandA.html
How can I append the .html extension automatically when needed when someone enters sitename.com/brands/* with no .html extension using the .htaccess document.

why don't you try this.
RewriteEngine On
RewriteRule ^/brands/(.*)$ /brands/$1.html [L]

You can use mod_negotiation for this, too:
<IfModule mod_negotiation.c>
Options +MultiViews
</IfModule>
If a request comes in, that would result in a 404, mod_negotiation takes other headers like the Accept into account and looks for files with the same base name but appropriate extensions.

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.

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.

How to redirect multiple similar URLs with one query?

I currently have lots of URLs that look like this:
http://www.domain.com/classes-dallas-tx.html
I need to change all URLs to the following:
http://www.domain.com/classes-in-dallas-tx.html
I just need to add the "in" into every URL that doesn't have it right now.
Is there one query I can use for the .htaccess file that will take care of all the URLs for me? I'm planning on using the 301 redirect.
Thanks!
Try:
RedirectMatch 301 ^/([^-]+)-([^-]+)-([^-]{2})\.html$ /$1-in-$2-$3.html
Or
RewriteEngine On
RewriteRule ^/?([^-]+)-([^-]+)-([^-]{2})\.html$ /$1-in-$2-$3.html [L,R=301]
in the htaccess file in your document root.

how to change folder location based on requested filename with mod_rewrite?

lets say url is requesting robots.txt like
http://domain1.com/robots.txt
and i want this url to look into http://domain1.com/domain1/robots.txt but keep the above url
any idea how to this is correct way? redirecting like this for robots.txt is allowed?
Regards
any idea how to this is correct way? redirecting like this for robots.txt is allowed?
Yes, just add this to your domain1.com document root .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^robots\.txt$ domain1/robots.txt [L,NS]
This will do an internal redirect to the file that you want. For more info see the Apache rewrite documentation.