How to make a pretty URL using .htaccess - apache

I am creating my whole application with .html extension, to play with the database I am using jQuery Ajax. I have created the project structure like WordPress, for each file I am having a folder and inside that folder I am having an index.html file.
In the above picture, I have created user/equipment/index.html, in this file all the equipment are being shown, now I want that if user clicks on an equipment then the URL should be like 'domain.com/user/equipment/equipment-title' and the file should be called user/equipment/details/index.html
I believe that this can be done with a .htaccess file.
Any solution for the problem would be much appreciated.

Well, you need to store the references in that index file the way you want them to be, request rewriting (wo which you refer as ".htaccess") cannot do that for you. Why you can do with request rewriting, so inside a distributed configuration file (".htaccess") is to the internally rewrite the incoming requests. For that you need a mapping from request URLs to your detail pages. If the mapping the simply the name as to be found in the "equipment" folder (this is unclear from your question), then you indeed can simply implement a rewriting rule.
This would be such an example:
RewriteEngine on
RewriteRule ^/?user/equipment/(.*)/?$ /equipment/$1 [END]
This will deliver the content of the file /equipment/equipment-title when the URL https://example.com/user/equipment/equipment-title gets requested and that file exists.
For this to work the rewriting module has to be enabled inside your http server and, if you want to use a distributed configuration file for this, the interpretation of such files also needs to be enabled for that location inside your http server. Usually the better alternative is to place such rules in the real http server's host configuration, though.

Related

Apache rewrite requests to build/public folder with .htaccess

I often have projects that produce some sort of output folder, usually named build or public or something like that. But if I deploy my entire project to an Apache web server, in order to route to the actual project you would have to do it like this: https://example.com/user/project/build/file.
To simplify and shorten the url, how can I rewrite requests in a .htaccess file so that if google for user/project/file it automatically routes it to user/project/build/file?
The best usually is to place only those files under the DOCUMENT_ROOT that are actually meant to be directly published by the http server. That also typically simplifies the URL to access them. The rest of the files, scripts and ressources should be kept outside the DOCUMENT_ROOT. You can still access them inside your application logic.
Sometimes this is not possible though. For example if you are using a cheap hosting service instead of operating your own http server. In such case request rewriting comes in as an alternative. In this situation you also typically have to rely on distributed configuration files (".htaccess") instead of being able to use the usual http server's host configuration. That comes with a few disadvantages, but sometimes it is your only option.
Have a try using such an implementation:
RewriteEngine on
RewriteRule ^/?user/project/build/(.*)$ /user/project/$1 [R=301,L]
RewriteRule ^/?user/project/(.*)$ /user/project/build/$1 [END]
This relies on the rewriting module being loaded and activated in the http server, obviously. And also the usage of distribute configuration files needs to be enabled for the http host and the requested location.
Always test using a fresh anonymous browser instance (to prevent caching issues in your tests). It also makes sense to start out using a R=302 temporary redirection first and only to change that to a R=301 permanent redirection once everything works as expected.

How to avoid displaying directory path in url?

I set up a little Apache2 server on a Raspberry PI4. Now I’m looking for a way to hide the real directory path displayed in the URL. I read around that you should deal with a file called .htaccess but, I don’t even know what to actually look for on the internet. How can I display an arbitrary url in the address bar of the browser, Hiding file extension like .php and file path?
You make rewrite rules in an Apache config file, a .htaccess file for example. One way you could achieve this is to create re-write rules in a .htaccess file. Use to below link to test your rewrite rules, then once you have that part working implement on your live apache installation.
https://htaccess.madewithlove.be/

How to implement url page redirection for a massive huge website

my site e.g. carparts.co.uk has 355000 unique urls. (it is a car parts catalogue site) (on webmaster tools it shows that 174000 of these are indexed)
We want to move our site to a new shopping cart platform (prestashop), and have completely changed the structure of the catalogue, which means we now have a new set of urls. (although the main domain is unchanged and is still carparts.co.uk)
i now have a excel sheet where I have a column of the 355000 'old' urls matched against the closest equivalent url on the new catalogue.
e.g.
old url: "carparts.co.uk/ford-ranger-alternator belts.htm"
goes to: "carparts.co.uk/belt-drive"
(and there are 355,000 of similar redirects)
my question is how should i do this?
i've that you can use htaccess to do this, but i'm worried because i've read that htaccess slows down sites if it is very large (is this slowness only encounted when trying to access one of the old urls?, or will it impact the speed of all my urls?
so what is the best thing for me to do with such a large number of urls?
Your best bet is probably setting up a RewriteMap. This requires server vhost config access as you can't configure the map from an htaccess file (though you can use one). The mapping is cached by apache so you don't need to worry about constant file access.
Something simple like:
RewriteMap redirects txt:/full/path/to/redirect-map.txt
Then in the file redirect-map.txt would simply have a "from" and "to":
"ford-ranger-alternator belts.htm" belt-drive
old-url.htm new-url
etc...
Then in either your htaccess file or in vhost config, just do:
RewriteCond $(redirects:$1|0) !=0
RewriteRule ^(.*)$ $(redirects:$1) [L,R]
Use of htaccess slows down the website because it needs to check several files for each request, and these are checked dynamically for every request.
It's more a problem for deep routed sites. For example, a request to:
www.example.com/folder1/folder2/folder3/folder4/index.htm
would need to check
The main config file.
Then add any overrides in the document root
htaccess file.
Then add any overrides in the folder1 htaccess file.
Then add any overrides in the folder2 htaccess file.
...etc.
However if you don't have deep nesting then it's not so bad. Still slower than not using them, but may not be noticeable on most sites.
The benefit of htaccess for you here, would mean that you wouldn't need to put all the redirects in one place, and could split them up amongst the htaccess file. I'm not sure of the impact of adding 355,000 redirects to the main Apache config, but it is a fair number, so imagine it could have a performance impact. The htaccess files, on the other hand, are read dynamically as the request is made, so all the redirects would not need to be loaded into Apache.
So, this might be one of the few use cases where htaccess might be a better solution, even if you do have access to the main config files.

Apache redirect for single XML file

I have a number of subdomains, which are using crossdomain.xml file and I'm looking to a simple way of managing them all - which get semi-regularly updated. One way I've thought is a PHP script, which pushes and overwrites the xml file. The other, which I much prefer is a an apache redirect on a single file.
So, question is how would I, across multiple domains, redirect an xml on dom1.domain.com and dom2.wirewax.com to the same crossdomain.xml file without Flash getting upset about. i.e. not a 302 HTTP redirect, but internal file fetching.
You can write a PHP script that fetches the content from a single location (database or text file) and sends it as-is to Flash. Yes, the script itself needs to be copied on all hosts.
If you have all websites hosted on same webserver, perhaps mod_alias could help:
Alias /crossdomain.xml /path/to/shared/crossdomain.xml
I have not personally tested this. The reference page includes instructions to setup the shared directory so that it can be read by multiple hosts.

how do i use htaccess to make http requests work properly

I currently have css and javascript file calls (amongst other things) like the following:
href="/css/default.css"
src="/js/ui_control.js"
putting the preceding / in to make the files relative to the root.
This works great when my page is in the root of the domain.
However, I'm currently in the middle of transferring my site to a new hosting provider and as such have a temporary URL which is: HOST-IP/~username
As such, all file calls are trying to be called from HOST-IP/css/default.css etc instead of within the ~username sub-folder.
Of course I can wait until the domain name servers propagate but that's beside the point.
How would I go about writing a rule in the .htaccess file that would redirect all file calls that start with a /, from going to HOST-IP/FILE-CALL, and instead to go to HOST-IP/~USERNAME/FILE-CALL. ?
Any ideas?
I'd suggest changing the references in your HTML to the files to be relative, as this will work either in a sub folder or as the root of the domain.
This works great when my page is in the root of the domain. However, I'm currently in the middle of transferring my site to a new hosting provider and as such have a temporary URL which is: HOST-IP/~username
How would I go about writing a rule in the .htaccess file that would redirect all file calls that start with a /, from going to HOST-IP/FILE-CALL, and instead to go to HOST-IP/~USERNAME/FILE-CALL. ?
Unless you can put a .htaccess at HOST-IP/.htaccess on the new server, you can't do this with .htaccess. It sounds like you're on a shared host, so any approach that'd let you do this with .htaccess would allow you to hijack everyone else's site on the server.