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

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]

Related

Hiding Directory in URL with htaccess is not working

I have website1.com setup so that when users visit website1.com they get redirected via a meta tag in an index.html to website1.com/directory
Then they use the website and go to links such as website1.com/directory/index.html or what ever. I am trying to hide the "directory" in the link so that users only see website1.com/index.html
I have place the htaccess which is to rewrite the url to website1.com/index.html at website1.com/directory/.htaccess
I am not doing anything else special and this should be an easy task. My current exact htaccess is as follows:
RewriteEngine On
RewriteCondition %{REQUEST_URI} !^directory/
RewriteRule ^(.*)$ directory/$1 [L]
Should be easy right..... All I get is a 404
In my server I have Mod_Rewrite & mod security and suspect one of them is causing this not to work but can't imagine why. Am I missing something here?
Your rule is incorrect since %{REQUEST_URI} variable has starting /
You need to place this in document root
Use this code in document root .htaccess:
RewriteEngine On
RewriteCondition %{REQUEST_URI} !^/directory/
RewriteRule ^(.*)$ /directory/$1 [L]

.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.

URL-rewriting with index in a "public" folder

I’m a newcomer in the development world. I desperately try to get the good URL. I checked the site for similar problems but I can’t find exactly what I need. Or I do it badly.
Here is the situation: I set up a project for a site whose the index.php file is in a folder named Public.
To be clearer, here is the URL I have now to reach the homepage of the built site:
http:// Domain Name.com/ Folder / Name of the site/public
My concern is about the folder Public: I don’t want it appears in the URL.
Here is the URL I’d like to get:
http:// Domain Name.com/ Folder / Name of the site
In fact, I’d like this URL permits to get the index file placed in the folder "Public".
I can’t access the Apache configurations (shared host) so I have a .htaccess I placed in the project (i.e: www/ Folder /Name of the site /.htaccess). Here is its content:
Options -Indexes +FollowSymLinks +Multiviews
RewriteEngine On
RewriteBase /public/
RewriteRule ^(.*)$ index.html [NC,L]
I made something very simple for now because I tried lots of things without efficient result.
Not really sure what you are trying to do, but if you want to remove the /public/ path that appears in the URL, you need to remove it from all your links, second, turn off multiviews, it's not what you want, third, you need a rule to externally redirect the browser when a request is made for /public/, then you need to internally rewrite requests to point to public.
Options -Indexes +FollowSymLinks -Multiviews
RewriteEngine On
# externally redirect, must match against %{THE_REQUEST}
RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /public/
RewriteRule ^/?public/(.*)$ /$1 [L,R=301]
# internally rewrite it back, but we must first check that it's pointing to a valid resource:
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -s
RewriteRule ^(.*)$ /public/$1 [L]

Rewrite /SUBDIRECTORY to /subdirectory

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.

redirect request uri to mirror site using htaccess

I have a website. I created a mirror of it and uploaded it in a directory called 'mirror'. What I wanted to do is whenever a viewer access for example..
http://www.example.com/this-page/another-segment/?id=1
I want him to be redirected to..
http://www.example.com/mirror/this-page/another-segment/?id=1
^^^^^^
(I am doing this because I want to want to edit my site's design but I don't want viewers to see the changes in progress until they are complete. Thus I want to redirect them to the mirrored snapshot, at least temporarily.)
Please suggest how this can be done using .htaccess or not.
after browsing around the internet. i came up with the idea of putting a /$1, a rewritebase and followsymlinks on the answer givien by appclay
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/mirror/
RewriteRule (.*) /mirror/$1 [R=301,L]
You'll want to do something like:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/mirror/
RewriteRule ^/(.*) /mirror/$1 [R]
In your .htaccess file.

Resources