.htacces redirect specific folder/subfolder - .htaccess

I am trying to make this work, please help me in it, if you can:
1.
I have a specific folder on an old domain, with a lot of subfolder.
Looking like this:
OLD-DOMAIN / FOLDER / something1 etc.
2.
I would like to redirect it to a new domain's specific ABCD folder.
NEW-DOMAIN / ABCD / FOLDER / something1 etc.
I have been using this code:
RewriteCond %{REQUEST_URI} ^/(FOLDER)$
RewriteRule ^ http://www.newdomain.com/**ABCD**/%1 [L,R=301,NC]
The code is working just fine for the first FOLDER, but not for the deeper subfolders. The /something1 doesnt redirecting through the chain.
How can I make this special redirect to every subfolders?
Thanks in advance!

Just change your rule to this:
RewriteRule ^FOLDER(/.*)?$ http://www.newdomain.com/ABCD/$0 [L,R=301,NC,NE]
Clear your browser cache before testing this change.

Related

redirect single image from one directory to another

I have one website, say www.example.com. So when I access "http://www.example.com/program/resources/foo.gif" it serves me foo.gif image from /usr/share/roundcube/program/resources/foo.gif, I found that in access log.
So what I would like to do is, I would like to copy that image from /usr/share/roundcube/program/resources/ to my webroot /var/www/www.example.com/webroot/img/ and write rewrite rule so that when request comes for foo.gif, it should serve from /var/www/www.example.com/webroot/img not from /usr/share/roundcube/program/resources/.
I've tried this
RewriteEngine On
RewriteRule /program/resources/(.*) /img/$1 [L]
It's working fine. But what if I want to make a rule for single image that is foo.gif ?
You can make it more generic and simpler than that.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/override%{REQUEST_URI} -f
RewriteRule .* override/$0 [L,NS]
Now just put any file you don't want to come from RoundCube into the override folder, e.g. override/program/resources/foo.gif

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]

.htaccess - How to hide the internal directory used by subdomains?

I installed a small FTP space for my classmates which allows them to upload school related documents to my server. After the files have been uploaded, they can be easily accessed via a subdomain i.e. ftp.mydomain.com
Everything is working fine, the files can be downloaded and everything works properly.
There are two things which still annoy me though:
If you enter ftp.mydomain.com you see Index of /my_internal_directory instead of something like Index of /
Once you enter a sub-directory and click the Parent Directory link it will redirect you to ftp.mydomain.com/my_internal_directory/ as well
Is there any way to hide my_internal_directory ?
This is the content of my .htaccess file so far:
RewriteEngine on
RewriteCond %{HTTP_HOST} ftp\.mydomain\.com
RewriteCond %{REQUEST_URI} !^/my_internal_directory/
RewriteRule ^(.*)$ /my_internal_directory/$1 [L]
Any help would be greatly appreciated.
If you are using linux server then you can remove read permission for folder my_internal_directory
If not resolve, try to add:
Options -Indexes
on top of the file.
If still doesnt resolve please post your .htaccess file... :)

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>

htaccess redirect from root to subfolder and then mask the url?

Two things:
Firstly - I have version 2 of a website located in a folder named v2, and I want to redirect any traffic that is NOT a child of the v2 folder, to www.example.com/v2
The old site located in the root was created in iWeb and has a LOT of subfolders and sub-subfolders.
So:
www.example.com/v2 = New site
www.example.com/Page.html
www.example.com/category/Page.html
ww.example.com/category/subcategory/Page.html = All generic examples of what I need to redirect.
Secondly, and I don't know if this is possible, I want to hide /v2/ in the URL, so that visitors will just see www.example.com/page even though they are actually on www.example.com/v2/page
Links are hardcoded to the v2 folder, like so <a href="v2/contact.html">
Any help is MOST appreciated. I've spent hours trying to figure this out, but I'm only just learning about htaccess and regular expressions, and am totally confused.
Thanks so much!
RewriteCond %{REQUEST_URI} !^v2/
RewriteRule ^(.*)$ v2/$1 [L]
rewrite everything by including v2 before it .
RewriteRule ^(.*)$ v2/$1 [L]
www.example.com/Page.html should now be processed as /v2/Page.html
You should not include v2/ in the url being sent to the user.
it would then become v2/v2/

Resources