Change Broswer URL, not actual paths or file locations - .htaccess

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

Related

Trying to modify display of my url for html page

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!

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"

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 *

.htaccess rewriting - URLs with multi level directories

I have a page deals.php which is placed at root directory that is being accessed by number of internal URLs with multi directory levels. for example this page would be accessed by both following urls.
http://domain/flights/asia/bangkok
http://domain/flights/bangkok
I am using this code in .htaccess to redirect to deals.php
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^flights/asia/([^/]+)/?$ deals.php?country=$1 [NC]
RewriteRule ^flights/([^/]+)/?$ deals.php?country=$1 [NC]
Now the problem here coming is that on deals.php all the images, script and style sheet files are not opening properly. If I try to fix it against one URL by placing ../../ in addresses of all images, script and css, it dont work for other URL.
How to solve this problem? what are the options?
Easy: DO NOT use ../ in links to any resources (images/css/js/etc) -- always use URL that is relative to the WEBSITE ROOT -- that is a "requirement" when you dealing with nice/not-real/rewritten URLs as such URL rarely points to the physical file location.
Lets assume you have a logo that is located at http://www.example.com/images/logo.png.
Now, instead of ../images/logo.png and/or ../../images/logo.png ALWAYS use /images/logo.png.
i think what you want is placing an baseurl in html page, so all the relative path will related to the baseurl, not the current URL in the navigator bar

Use htaccess to mask folder name

Here's a problem I'm always wanting to solve with htaccess. I haven't found a way yet, though it looks like it should be possible - perhaps someone can help.
Let's say I have a folder at the root of my site called /foo/. I want users to be able to access that folder at the path /bar/, but for various reasons I can't rename the folder.
So as not to create confusion I only want one path to ever be seen - that is to say, I don't want people to use the name /foo/ to access the folder; they should always use /bar/. If someone goes to example.com/foo/, their browser should redirect to example.com/bar/ - but the content returned should be the content of /foo/.
To make matters more complicated, pages in /foo/ have dependencies (images, stylesheets, links to other pages, etc) within /foo/ which are hardcoded and can't be changed. These must, of course, still work.
So, to summarise, this is what I want :
Requests for example.com/foo/ should redirect to example.com/bar/.
Requests for example.com/bar/ should return the contents of example.com/foo/.
Is this possible? It looks on the surface as if it would create an infinite redirect... but I'm pretty sure there are ways to prevent that in htaccess, aren't there?
I'd be very grateful for any help.
(PS - for a little extra background: The normal reason I want to do this is to rename the wordpress /wp-admin/ directory to something more professional and easy for customers to remember, such as /admin/. But the same system should work for masking any path in this way.)
I found a sort of workaround - by using a symlink and htaccess in combination.
First I created a symlink from /bar to /foo/.
Then I put this in htaccess :
Options +FollowSymLinks
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
This has exactly the desired result - example.com/bar/ shows the content of the /foo/ directory, and example.com/foo/ redirects to example.com/bar/
But if anyone can come up with a pure htaccess solution I'd much prefer that!
Update :
Ok, I've finally found out how to do this. It turns out to be quite simple...
RewriteCond %{THE_REQUEST} ^GET\ /foo/
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
RewriteRule ^bar/(.*)$ foo/$1
The only problem is that it doesn't take account of RewriteBase, so you have to include the full path in the first line (after ^GET\).
If I understand correctly what you want is something like this inside your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^foo/$ bar/
RewriteRule ^bar/$ foo/
</IfModule>

Resources