Trying to modify display of my url for html page - .htaccess

What I would like to do is serve up a file such as "boat-repair" and have the url read allservicemarine.com/boat-repair/electrical-system-repair
Everything I have found so far seems to reference PHP files as part of the process is this possible with an html file and if so how can I potentially accomplish it
This code:
RewriteEngine On
RewriteRule ^/?$ /boat-repair.html [NC,L]
Will serve up the file when you type in allservicemarine.com
I tried putting the target file in a series of sub directories hoping the directory names would show in the address bar,but that still just pulls up the target file. I have been searching for many hours to try to find a solution and would appreciate it if anyone could point me in the right direction to accomplish the task.

This is the working code I came up with:
RewriteEngine On
RewriteRule ^/?$ /boat-repair/electrical-system-repair [NC]
RewriteRule ^boat-repair/electrical-system-repair/?$ /index.html [NC]
The first rule tells the server to serve up: boat-repair/electrical-system-repair when the root directory is entered.
The second rule tells the server if the pattern: boat-repair/electrical-system-repair is matched to serve up index.html
The file served up (index.html) in the last step can be named anything you like as it will load, but not show in the address bar.
So after both rules are implemented you get: http://yoursitename.com/boat-repair/electrical-system-repair/ in the address bar, now I just need to figure out a way to remove the trailing slash.
These rules of course get put in the htaccess file.
In order for this to work you must have the folders named in the rules even if they are just dummy folders. If dummy folders be sure to put a text document in each folder named "DO NOT DELETE THIS FOLDER" so that six months down the road you do not look at these empty folders and delete them!

Related

Change Broswer URL, not actual paths or file locations

I'm running apache2 on Linux and I've not been able to find a solution to this.
I want to change the URL shown in the browser, but not change any real paths etc for files.
Eg:
http://127.0.0.1/school/northern/
http://127.0.0.1/school/southern/
I'd like the URL to appear as the following for both:
http://127.0.0.1/school/
But I don't want to change any paths to relating to northern or southern, they must remain as is.
I've tried adding a .htaccess in the northern folder as follows:
RewriteEngine On
RewriteRule ^(.*)/(.*) /$1 [L,R=301]
This forces a redirect back to http://127.0.0.1/school and the files aren't there so it fails.
Is this possible ?
Thanks

Clean URL rewriting rule

right now my url looks like this:
http://domain.com/en/c/product%2C-product2%2C-product3/82
where last number is category numer.
And im trying to rewrite it and redirect user to url which should look this one:
http://domain.com/82/product-product2-product3
The clue is I want to hide "en/c/" part and clean url from commas and blank spaces. I'm completely green in rewriting.
Any ideas?
You can use these 2 rules in your root .htaccess for that:
RewriteEngine On
RewriteBase /
RewriteRule ^en/c/([^,\s]*)[,\s]+([^,\s]*)/(\d+)/?$ $3/$1$2 [NC,L,NE,R=302]
RewriteRule ^(en/c)/([^,\s]*)[,\s]+(.*)/(\d+)/?$ $1/$2$3/$4 [NC,L]
In order for this to work, we need to tell the server to internally redirect all requests for the URL "url1" to "url2.php". We want this to happen internally, because we don't want the URL in the browser's address bar to change.
To accomplish this, we need to first create a text document called ".htaccess" to contain our rules. It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). This would be placed in the root directory of the server (the same folder as "url2.php" in our example). There may already be an .htaccess file there, in which case we should edit that rather than overwrite it.
The .htaccess file is a configuration file for the server. If there are errors in the file, the server will display an error message (usually with an error code of "500").
If you are transferring the file to the server using FTP, you must make sure it is transferred using the ASCII mode, rather than BINARY. We use this file to perform 2 simple tasks in this instance - first, to tell Apache to turn on the rewrite engine, and second, to tell apache what rewriting rule we want it to use. We need to add the following to the file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^url1/?$ url2.php [NC,L] # Handle requests for "url1"

How do I do a htaccess rewrite to another folder for a single file?

We moved a part of our site from one sub folder to another. I want to put permanent redirects (301) into htaccess for the files in this folder (some have changed their filename as well, so I can't just setup one rule for the whole folder). Here's what I'm trying
RewriteRule ^search/tutorial-search.html$ db/tutorial.php [R=301]
This doesn't work though, I get a 404 response when now entering the old URL. I find this curious as I had a rule in place for ages that does work, which looks like this:
RewriteRule ^search/tutorial-search.html$ search/tutorial-search.php
I really don't see the big difference. I also tried the following (among others) but it doesn't work either
RewriteRule ^search/tutorial-search.html$ db/tutorial.php
What exactly is causing this to fail? Just to make sure I put all of these at the exact same line of the htaccess file. Is it because I'm rewriting to another folder? Thanks :)
Try adding a leading slash to your rewrite targets, because when redirecting, apache could be mistaking a URL-path with a file-path.
RewriteRule ^search/tutorial-search.html$ /db/tutorial.php [R=301]

htaccess get content from subfolder

I have searched a lot but cannot find the information i want. I have a domain http://mydomain.com, my html files are nested in 3 level subfolders (for example thingy.html is nested in t/h/i/ folder, testy.html nested in /t/e/s/ folder
Now i want to type http://mydomain.com/testy.html or http://mydomain.com/thingy.html in the browser, it should show the content of those files without being redirected to http://mydomain.com/t/h/i/thingy.html and http://mydomain.com/t/e/s/testy.html
Is this doable?
Could anyone please help me? I'm new to .htaccess
Unless there's some kind of deterministic whay these 3 level subfolders are named, you'll have to do each one explicitly and separately. For example, the the htaccess file in your document root, you can have:
RewriteEngine On
RewriteRule ^/?thingy.html$ /t/h/i/thingy.html [L]
RewriteRule ^/?testy.html$ /t/e/s/testy.html [L]
etc.

simple .htaccess rewrite URL to different directory on same server

Ok, I'm clueless here...
I need to rewrite a directory structure and all sub-directories within it to a directory within the same server, but a root that is before the directory.
For example:
http://www.mydomain.com/Themes/default/css/folder
and all directories called upon after folder. Such as folder/sub_folder or folder/afolder/anotherfolder, it needs to include ALL sub-directories within the folder directory.
should be redirected to this:
http://www.mydomain.com
How do I do this via a .htaccess file within the folder path http://www.mydomain.com/Themes/default/css/folder?
Please someone help.
Thanks guys :)
The files within the directory structure still need to be accessible for that structure when called via PHP, but I don't want people being able to browse to http://www.mydomain.com/Themes/default/css/folder and be shown all subdirectories within that folderpath and/or all files. Same thing for all sub-directories that follow that folder path.
I'd like to be able to place the .htaccess file within the http://www.mydomain.com/Themes/default/css/folder directory on the server, but don't know exactly what code to use for this.
ALSO, even more challenging... The domain name can change, so I'd rather not use the domain name within the .htaccess file, instead perhaps use .. or . to go up a directory or a different method of grabbing the domain name within the .htaccess file.
Create a .htaccess file in /Themes/default/css/folder and place these lines there (it requires mod_rewrite):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ http://%{HTTP_HOST}/ [R=301,L]
It will redirect (301 Permanent Redirect) all requests to a folder to a homepage. If file is requested, it will allow it.
If you want to have it working for folders as well as files then remove the RewriteCond line -- it will redirect ALL requests (even for non-existing URLs) to a homepage.
If you will see "500 Internal Server Error" after creating such file, then it is your server configuration: mod_rewrite may not be enabled or it's directives (RewriteRule, RewriteCond, RewriteEngine) are not allowed to be placed in .htaccess. In any case -- check Apache's error log for exact error message (it will give you the exact reason).
http://www.besthostratings.com/articles/prevent-directory-listing.html
IndexIgnore *

Resources