How can I hide folder name from URL using .htaccess - .htaccess

I have a website with the root folder 'www', but I put all php files including index.php in a sub-folder of root.
I wrote myself a .htaccess file to redirect, so if I input www.test.com, it will jump to www.test.com/folder and display the index.php.
Below it's my .htaccess which I put in the root.
RewriteEngine On
RewriteBase /
RewriteRule ^folder2 - [L]
#ignore folder2 in which I put important files, but no works for the website
RewriteCond %{REQUEST_URI} !^/folder(.*)
RewriteRule (.*) /folder/$1
Right now, I want to change the .htaccess file to reach these goals:
When I input www.test.com, jump to www.test.com/folder as usual, but display the url without folder.
All the pages in folder will display the url without folder name.
Such as
www.test.com/shop/page1 -> www.test.com/page1
I searched some of the scripts, but none of them works.

I've found this related question which answers to your problem. The snippet from the answer is (note: it's adjusted to your needs):
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^folder/)^(.*)$ /folder/$1 [L,NC]
Reference: https://stackoverflow.com/a/18361995/3673491

You can use below code.
RewriteEngine On
RewriteBase /
RewriteRule ^/?$ /folder/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/$1 [L]

Based on your current rules. You can just make a slight adjustment.
RewriteEngine On
RewriteBase /
#ignore folder2 in which I put important files, but no works for the website
RewriteRule ^folder2/? - [L]
RewriteRule ^/?$ /folder/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/$1 [L]

Related

edit htaccess to get inside a folder for rewrite rule

After some help from another question i managed to figure out about .htaccess on my website for Friendly SEO links.
My public_html folder contains those files
index.php
.htaccess
buisnessdetails.php
eventDetails.php
My htaccess so far is this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)\ (.*)$ /$1-$2 [L,R=301]
RewriteCond %{THE_REQUEST} /eventDetails\.php\?id=(.+)&name=(.+)\sHTTP [NC]
RewriteRule ^ /%1/%2? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /eventDetails.php?id=$1&name=$2 [L]
</IfModule>
So when someone clicks an href which has http://sourtouki.gr/123/abc
the htaccess file goes to the
eventDetails.php file.
But now i want it to change like this
i've edited my public_html folder like this
index.php
events(folder)
2.1 eventDetails.php
buisness(folder)
3.1 buisnessdetails.php
So with these changes i want to do the following thing
Changed the href link to
http://sourtouki.gr/events/123/abc
What changes i must do to the .htaccess file so it can understand that if someone pushes the above link, to go to the eventDetails.php which is inside events folder??
And is it going to be editable so i can add also buisness folder inside that rewrite rule?
You can use something like :
RewriteEngine on
RewriteRule ^(.*)\ (.*)$ /$1-$2 [L,R=301]
RewriteCond %{THE_REQUEST} /events/eventDetails\.php\?id=(.+)&name=(.+)\sHTTP [NC]
RewriteRule ^ /events/%1/%2? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^events/([^/]+)/([^/]+)/?$ /events/eventDetails.php?id=$1&name=$2 [L]

reroute htaccess when index in any subfolder

I currently have the following rewrite set up in my htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/(.*)$ index.php?controller=$1&cmd=$2&params=$3 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)$ index.php?controller=$1&cmd=$2 [L,QSA]
RewriteRule ^([^/]*)$ index.php?controller=$1 [L,QSA]
So for example blog just rewrites to index.php?controller=blog or blog/show/32 rewrites to index.php?controller=blog&cmd=show&params=32 etc etc.
I want to shift all my site into a subfolder called testing so need to update my htaccess to suit. Not worried about rerouting to the folder i.e. i'm not bothered about typing www.example.com and it landing at /testing/index.php, I want to type www.example.com/testing/ and it land at index.php which it does but the other rules seem to break.
If you move this .htaccess to the subfolder, together with the files, it will work just as expected. You don't need to change anything. If you have a RewriteBase directive in your .htaccess, you have to update it to reflect the new path.
Keep this rule in your root .htaccess:
RewriteEngine On
RewriteRule ^((?!testing/).*)$ testing/$1 [L,NC]
Keep this rule in your /testing/.htaccess`:
RewriteEngine On
RewriteBase /testing/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/(.+)$ index.php?controller=$1&cmd=$2&params=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?controller=$1&cmd=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?controller=$1 [L,QSA]

.htaccess - This webpage has a redirect loop

I'm cleaning my URLs and everything looks fine but whenever I try to access any directory such as images etc then chrome shows that "This webpage has a redirect loop." However I want to protect directories inside public_html.
The .htaccess file is inside public_html
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
</ifModule>
I want to protect images, css and javascript directories but want to allow access to the admin directory.
Thank in advance.
Change order of your rules and change your trailing slash removing rule:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]
# block all directories except admin/
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !^admin(/|$) - [NC,F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</ifModule>
You can use this remove all code and use this one
option index allow and disallow to be index directory like
This does the trick.
Options All -Indexes
or
IndexIgnore *
for more information please check below link
http://viralpatel.net/blogs/htaccess-directory-listing-enable-disable-allow-deny-prevent-htaccess-directory-listing/

.htaccess redirect certain virtual directories

/podcast/wp/ is a folder, everything else is a virtual directory already generated by RewriteEngine. Here's the code provided by WordPress:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /podcast/wp/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /podcast/wp/index.php [L]
</IfModule>
I would like to redirect all requests in the wp/ directory (except for existing folders)
excluding the following possible paths (also virtual directories):
/podcast/wp/ANYSTRING1/ANYSTRING2/feed
to another domain:
example.com
using .htaccess while the excluded path remains working as is.
The goal is to "hide" (redirect) the entire WordPress blog except for the feeds.
Thanks for your help!
Change the wordpress generated rules to:
RewriteEngine On
RewriteBase /podcast/wp/
# new stuff
RewriteCond %{REQUEST_URI} ![^/]+/[^/]+/feed$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://example.com/ [L,R]
# original wordpress stuff
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /podcast/wp/index.php [L]
Depending on how you want to handle the redirect, you can tweak the rule that redirects to http://example.com/. If you want 301 permanent redirects, add a 301:
RewriteRule ^(.*)$ http://example.com/ [L,R=301]
If you want to preserve the relative URI in the redirect, use a backreference:
RewriteRule ^(.*)$ http://example.com/$1 [L,R]
If you want to preserve the entire URI (including the /podcast/wp/ part, use the URI:
RewriteRule ^(.*)$ http://example.com%{REQUEST_URI} [L,R]

.htaccess redirect woes — not completing

So, at the moment, my .htaccess looks a little like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{HTTP_HOST} ^colorspace\.am$ [OR]
RewriteCond %{HTTP_HOST} ^www\.colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ "http\:\/\/i\.colorspace\.am\/portfolio$1" [R=301,L]
I want to move all the content from my root directory into a subdomain (which I've done) but I don't want the links people have to not work. www.colorspace.am/portfolio needs to redirect to i.colorspace.am/portfolio, and all the sets contained therein (ie /portfolio/YYYYMMDD) also need to be 'adjusted' on the fly (www.colorspace.am/portfolio/YYYYMMDD -> i.colorspace.am/porfolio/YYYYMMDD
NOTE: i.colorspace.am contain's 2011's content; ii.colorspace.am will contain 2012. They're two entirely different WP installs with their own respective databases. Not sure if it's relevant but..
What seems to be happening is that /portfolio is instructed to redirect to i.colorspace.am/portfolio, but for whatever reason it's ending up at i.colorspace.am
IF there's a way I can make any www.colorspace.am/folder/sub-folder redirect to i.colorspace.am/folder/sub-folder (wildcard?) ..that would be amazing. But I'd be just as happy with a single fully working redirect at this point.
NOTE: the redirect code was generated by my administration panel. I tried
Redirect /portfolio http://i.colorspace.am/portfolio
But it resulted in a 'too many redirects' error.
After the redirection from
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
The rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Is accessed again. That is the reason it's ending up at i.colorspace.am.
Add these 2 rules
RewriteCond %{HTTP_HOST} ^(?:www\.)?i.colorspace\.am$
RewriteRule protfolio -[L]
in order mentioned below.
also change (just a small optimization)
RewriteCond %{HTTP_HOST} ^colorspace\.am$ [OR]
RewriteCond %{HTTP_HOST} ^www\.colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
to
RewriteCond %{HTTP_HOST} ^(?:www\.)?colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
and put it inside of IfModule block in the same order mentioned below.
Have just the below in your .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#add these 2 lines:
RewriteCond %{HTTP_HOST} ^(?:www\.)?i.colorspace\.am$
RewriteRule protfolio -[L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
</IfModule>
# END WordPress

Resources