Rewrite /SUBDIRECTORY to /subdirectory - .htaccess

I want to make it so that if a visitor navigates to:
http://www.website.com/folder
They get redirected to:
http://www.website.com/FOLDER
It is only for this specific folder that I want to affect (or intend to).
I have tried:
RewriteEngine On
RewriteBase /FOLDER/
RewriteRule ^folder(/.*)?$ /FOLDER$1 [L,R=301]
But I don't think that is right

In the htaccess file in your document root,
RewriteEngine On
RewriteRule ^folder(/.*)?$ /FOLDER$1 [L,R=301]
should be good enough. You don't need the rewrite base but I don't think that'll affect it.

Related

.htaccess forward from one domain to a specific .html page

I know how to use .htaccess to forward everything in one domain to a new domain name. But in this case, I want everything from one domain to go to a specific .html page on a different domain. That's where I'm lost. I'm trying the following but it just redirects to a folder and the page in question is in that folder but obviously, I don't want people seeing the contents of that folder. Make any sense? So example.com needs to go to yyy.com/some-page.html
This is what I'm currently using:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} (www\.)?5\.xxxx\.com [NC]
RewriteRule ^(.*)$ http://www.1.yyy.com/$1 [R=301,L]
Try:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} (www\.)?5\.xxxx\.com [NC]
RewriteRule .* http://www.1.yyy.com/some-page.html [R,L]
You can also put a blank index.html page to the directory in question to mask its contents or
you can put index.php file with this code <? header ("location: http://www.1.yyy.com/some-page.html"); ?> that will redirect a user to the desired page.
$1 is a place holder for the 1st pattern match. So if you are rewriting domaina.com/someurl/, it is attempting to load domainb.com/someurl/. Swap the $1 with the actual page --- e.g. somepage.html and it should work. But unless both of these domains are pointing to the same files/directories, the rule seems a bit overcomplicated.
So how about just a simple redirect?
Try this in your .htaccess file.
redirect 301 / http://somesite.com/somepage.html
OR you can try this.
RewriteRule ^(.*)$ http://somesite.com/somepage.html [R=301,L]
It does work and you can test my RewriteRule below.
http://htaccess.madewithlove.be/
There must be something else going on.

htaccess redirect if filename equals something

I'd like to set a redirect (preferably with RewriteCond) so that if the requested file is index.php regardless of directory, it will be redirected to another site.
So visiting /index.php or/files/index.php or /stuff/index.php (etc.) will all redirect you to another domain.
Here is a general way to do it. This rule set should be placed in the .htaccess file in the root directory:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} index\.php/? [NC]
RewriteRule .* http://otherdomain.com/ [R=301,L]
Redirects permanently any URL that holds index.php in any position, like
http://mydomain.com/index.php or
http://mydomain.com/any/folder/quantity/index.php or
http://mydomain.com/any/folder/quantity/index.php/any/folder/quantity/
To
http://otherdomain.com/
That's it. You don't explain much so nothing is passed to the other domain, just as you say in your question:
...redirected to another site.
These rules should do it (when placed inside /.htaccess file):
RewriteEngine On
RewriteRule (?:^|/)index\.php$ http://otherdomain.com/ [R=301,L]

modrewrite website with slash in .htaccess

How can I translate an URL like:
http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAIL
to:
http://localhost/mySite/OFFERS/ARTICLE/DETAIL
if one parameter is empty it should still work like this:
localhost/mySite/?link=OFFERS&sublink=ARTICLE
localhost/mySite/OFFERS/ARTICLE
Extra problem: There is an enter page under index.php and the rewrite should work with index2.php. Best would be if it would work under localhost and on live system without changes.
Currently I'm using: RewriteRule ^([^/.]+)$ index2.php?link=$1 [L]
But that only works for one parameter and I couldn't improve this code for ages ^^
RewriteRule ^([^/.]+)(/([^/.]+))?(/([^/.]+))?$ index2.php?link=$1&sublink=$3&subsublink=$5 [L,QSA]
Note that localhost/mySite/OFFERS/ARTICLE links to localhost/mySite/?link=OFFERS&sublink=ARTICLE&sussublink= and not localhost/mySite/?link=OFFERS&sublink=ARTICLE.
Should not be a big issue, but make sure the PHP code doesn't us isset($_GET['subsublink']).
Try adding the following to your htaccess file in the mysitedirectory of your site.
RewriteEngine on
RewriteBase /mysite/
# rewrite http://localhost/mySite/OFFERS/ARTICLE/DETAIL to
# http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAIL
#assumes that index2.php is in the root directory of site
RewriteRule ^([-a-zA-Z0-9])+/([-a-zA-Z0-9])+/([-a-zA-Z0-9])+ index2.php?link=$1&sublink=$2&subsublink=$3 [NC,L]
#redirect index to index2
#if you do not want to redirect, just server rewrite, remove the R=301 below
RewriteRule ^index\.php$ index2.php [NC,L,R=301]

How do I use .htaccess to change the structure of my website?

I'm doing some reorganizing on my web server and would like to change the way my structure works. Currently, everything is stored at the lowest level so that when I access my site the url looks like this:
www.example.com
www.example.com/page1.php
I have many pages in my site and i would like to move them out of the main folder. eg.
www.example.com/folder/
www.example.com/folder/page1.php
however, I would like for users not to see the /folder/ section of the url. In other words, when a user visits my site, I want him to navigate to:
www.example.com/page1.php
www.example.com/myfolder1/page1.php
www.example.com/myfolder2/page2.php
www.example.com/anyfoldername/anypagename.php
but actually be at
www.example.com/folder/page1.php
www.example.com/folder/myfolder1/page1.php
www.example.com/folder/myfolder2/page2.php
www.example.com/folder/anyfoldername/anypagename.php
I want the url to show without the /folder at all times.
Is there a way to do this with the .htaccess file?
You can easily do this using mod_rewrite. For example:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^page1\.php$ /folder/page1.php [L]
NOTE:
This is to be placed in .htaccess in your website root folder. If placed elsewhere some small tweaking may be required.
UPDATE:
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ /folder/$1 [L]
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]*)$ folder/$1 [L]

How do I fix old links after modifing .htaccess for Joomla installation in a subfolder?

I have my main Joomla installation in a subdirectory. I used to redirect users from www.mysite.com to www.mysite.com/subdir with a 301 so that the live site was entirely dislocated over there.
I don't actually like the fact that all the URL are preceded by the subdirectory /subdir/ (and I also think this is not very good for SEO) so I modified my .htaccess file like this:
RewriteEngine On
RewriteBase /
# Add trailing slash if path does not contain a period or end with a slash
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) http://www.mysite.com/$1/ [R=301,L]
#Change http://yoursite.com to http://www.mysite.com (Optional)
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?(.*)$ http://www.mysite.com/$1 [R=301,L]
#Rewrites http://www.mysite.com/subdir to http://www.mysite.com/
RewriteRule ^(.*)$ subdir/$1 [L]
I also edited the configuration file for Joomla! so that now all the links in the site point (correctly) to www.main.com/theirquery and noto to www.main.com/subdir/theirquery
Now, however, all the old links (that have been posted to other webistes, for example) appears to be broken (404): how can I solve this?
I think I have to redirect (301) them to the new subdirectory-free address, that will be (another time) silently redirected with the htaccess I posted.
But I don't know how to do this!
Thank you in advance!
Can you try setting the $live_site parameter in configuration.php? You need to edit it directly rather than through the backend of Joomla.
You need to add this to your htaccess.
RewriteRule ^subdir/(.*)$ /$1 [R=301,NC,L]
This is not possible since the old links where not physical but stored in a database, so redirecting them wouldn't be possible without passing through the database again.

Resources