Hiding subfolders from URL through .htaccess - .htaccess

How do I hide all subfolders from the URLs on my site?
So http://www.example.com/whatever just shows as http://www.example.com.

Just put this into you htaccess:
RewriteEngine On
RewriteRule ^ index.php [L]
All your request will be rewritten to index.php, which will achieves what you're describing.

Related

Stop .htaccess from redirecting root to root/index

I have somehow managed to redirect my site from domain.com/ to domiain.com/index and I was hoping someone could take a look at my .htaccess code to see where I have gone wrong.
Quick background story
I had an old website where all files were html
I then bundled all those files into a folder called old and told all
bots via robots.txt not to crawl it. robots.txt remains in the root
folder
A bunch of .php files were then added to the root folder. Each file has its own rel="canonical" tag with href="http://domain-name.com/file-name" - the homepage rel="canonical" ends with /index
Inside the root folder, I created a .htaccess file that:
Redirected .html files to the correct .php file
Redirect index.php to root (this does not seem to work)
Redirect www to non-www
Remove php file extension
I've tried to re-order the code in case I'm, for example, trying to remove extensions before properly redirecting but nothing seems to work.
I've tried playing around with how I'm redirecting index to index and nothing seems to work.
To see what the redirect path looks like I opened Chromes developer tools and selected network. From there I typed in
http://www.domain-name.com/index.html
And the redirect path looked like this
http://www.domain-name.com/index.html
http://domain-name.com/index.html
http://domain-name.com/index (this is my index.php file)
My htaccess code
301 Redirects Old HTML over to new PHP
Redirect 301 /index.html http://domain-name.com/index
Redirect 301 /about_us.html http://domain-name.com/about
Redirect 301 /services.html http://domain-name.com/services
Redirect index.php to root
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://domain-name.com/$1 [R=301,L]
Redirect www to non-www
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain-name\.com [NC]
RewriteRule ^(.*)$ http://domain-name.com/$1 [L,R=301]
</IfModule>
remove php file extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
I'm not sure what I have done wrong but I'm hoping you can help me understand why the homepage is redirecting to
http://domain-name.com/index
Thank you for your time
Change Redirect 301 /index.html http://domain-name.com/index
to Redirect 301 /index.html http://domain-name.com/
File index.php is your DirectoryIndex so request to http://domain-name.com/ load /index.php
If not, please add to top of your .htaccess file directive: DirectoryIndex index.php

URLs Get redirected to root for webroot files

I have a CakePHP app that has some html static files in webroot.
HTML files are in some directories like webroot/dir1/dir2/page.html.
When I enter url: site.com/dir1 I get redirected to site.com/webroot/dir1/ but when I enter site.com/dir1/ I get redirected to the first page site.com
And If I enter url for the html file: site.com/dir1/dir2/page.html I get redirected to the first page site.com. It supposed to show the html file. How can I make it show html file and not redirect to first page?
In my public_html the htaccess is:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
In the webroot the htaccess is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
It's definitely not connected to CakePHP. Just ignore your directory / ies that should not be processed by CakePHP's routing system, see this related SO post.
Insert .htaccess into your directory/ies that should be ignored:
RewriteEngine Off
your questions isn't about php,
it's about the apache Webserver or more about -> Apache Module mod_rewrite
Rewrite Engine is only called one time, so you have to put all changes in one File.
kind regards
set

301 redirect a folder but exclude child folder

I need to redirect a directory "mysite.com/blog" to a new site. However, I do NOT want to redirect a child folder "mysite.com/blog/wp-admin".
Here is my htaccess code which effectively redirects the entire /blog folder.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^blog(.*) http://www.newsite.com/newblog [L,R=301]
</IfModule>
I have tried the following to exclude /wp-admin, but that didn't work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/blog/wp-admin(.*)
RewriteRule ^blog(.*) http://www.newsite.com/newblog [L,R=301]
Any ideas? Thank you!
UPDATE:
I tried removing the (.*) from both lines, as directed, and its still redirecting to the new site. BUT, the URL is different. Here's the URL once it redirects:
http://www.newsite.com/newblog/?redirect_to=http%3A%2F%2Foldsite.com%2Fblog%2Fwp-admin%2F
Can you tell from this URL what's going wrong? I'm not good at regex, but it looks like it's a formatting issue.

.htaccess to partially redirect index requests

I'm doing some work for a friend on their website, and while we are working on the new site I would like to set up their .htaccess file to redirect users with the following rules:
Redirect any requests to / or /index.html to a subdirectory (e.g. both http://example.com/ and http://example.com/index.html should redirect to http://example.com/legacy/index.html)
Allow direct requests to index.php to pass through without redirect (e.g. http://example.com/index.php)
If possible, requests to a second subdirectory get redirected to index.php (e.g. http://example.com/beta/ redirects to http://example.com/index.php)
This is probably a simple request, but I have no experience with the rules language that .htaccess uses.
Thanks in advance!!
Redirect any requests to / or /index.html to a subdirectory (e.g. both http://example.com/ and http://example.com/index.html should redirect to http://example.com/legacy/index.html)
RewriteEngine On
RewriteRule ^(index\.html)?$ /legacy/index.html [L]
Allow direct requests to index.php to pass through without redirect (e.g. http://example.com/index.php)
RewriteRule ^index\.php$ - [L]
If possible, requests to a second subdirectory get redirected to index.php (e.g. http://example.com/beta/ redirects to http://example.com/index.php)
RewriteRule ^beta/?$ /index.php [L]
These would all go in the htaccess file in your document root. If you actually wanted to redirect the browser so that the URL in the address bar changes, include a R=301 in the square brackets: [L,R=301]. If when you say "requests to a second subdirectory get redirected to index.php" meaning "any subdirectory", then the rule needs to be changed to:
RewriteRule ^([^/]+)/?$ /index.php [L]
i kind of did the reverse with codeigniter ...
RewriteEngine On
RewriteBase /myproject
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
$config['base_url'] = 'http://localhost/myproject/';
$config['index_page'] = '';
hope this helps

mod_rewrite from .php to .html for web crawlers

I am using mod_rewrite in .htaccess to redirect from .php to .html like this:
RewriteRule ^([^.]+).html$ /$1.php [QSA,L]
It works in a web browser, but Google is crawling the old URLs i.e. index.php and faq.php.
How I can redirect the index.php to index.html? My website URL is: http://www.21flats.com/.
Add [QSA,L,R=301] so Google will see that there has been a redirect. This adds a 301 redirect to the rewrite for search engines:
RewriteRule ^(.*)\.php$ /$1.html [QSA,R=301,L]
You have the redirect backwards, so change it and add the 301 rule and you are good.
You can try something like this:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L]

Resources