htaccess get content from subfolder - .htaccess

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.

Related

Writing htaccess that points to a file in the sub-directory

I have a order page in the following path
https://example.com/backend/web/order
And I want to display it as
https://example.com/order
What should be the htaccess code (please let me know of every step if possible so I can learn also). Where should I place the htaccess file? Inside the backend folder or the root folder.
To change https://example.com/backend/web/order to https://example.com/order you can use the following rule in htaccess in your root folder :
RewriteEngine On
RewriteRule ^order /back-end/web/order [L]
The rule above makes it possible to access your old URL as http://example.com/order .

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!

Show index page for all links to folder

I have a folder named "keywords" for my website, such as this: http://www.mysite.com/keywords/
In the folder I only have an index.php file. Regardless of what someone types in after the folder, I need the index.php to show. For example, http://www.mysite.com/keywords/this-is-a-test should show the index.php file's content but without changing the URL.
Everything I've tried seems to fail - I'm working with the .htaccess in my /keywords/ folder. Regardless of what I put there I seem to keep getting a 404 error. Any help would be appreciated, I'm sure it's something small and simple that I just don't know how to do.
This is what I currently have:
RewriteEngine on
RewriteRule - index.php [L]
The problem with this is that it only work when going to links with a dash in them.
For example,
http://www.mysite.com/keywords/testing-this
does work. But this does not work:
http://www.mysite.com/keywords/testingthis
Any help would be appreciated.
RewriteEngine On
RewriteBase / #make sure your website is located in root directory, if not you have to add directory here...
RewriteRule ^keywords/testingthis$ /keywords/index.php$1 [L]

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 *

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