2

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:

  1. When I input www.test.com, jump to www.test.com/folder as usual, but display the url without folder.
  2. 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.

2

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

I should replace this file with my .htaccess in the root, am I right? The sites only jump to the folder, but url still be 'www.test.com/folder'.
Note that I just updated the code with the answer from @NinjaCat suggestion, again adapted to your needs. Check it out and tell me if it's working properly now. I've made a simple local test here and it worked. Note that you need to change wherever you see folder with the actual name you want to give to that folder.
1

You can use below code.

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

Comments

0

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]

3 Comments

It's not working. The website does not even show the index.php page any more.
It's still not jumping. Should I put this file in the folder or just keep it in the root?
What do you mean not jumping? I don't understand what you mean by that. What are you typing in the browser and what do you expect to happen? Start there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.