htaccess remove folder from url only - apache

im trying to remove only a specific folder from my url. i have see many answers here but i cant find any to work for me.
i have this url http://www.mydomain.com/projects/books/index.php
i want to just remove the projects folder so i can use
http://www.mydomain.com/books/index.php
i have this .htacces inside public_html folder :
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)projects
RewriteRule ^(.*)$ projects/$1 [L]
with this code i can access http://www.mydomain.com/books/index.php but any other address redirect me inside the projcets folder
for example http://www.mydomain.com shows me the index of/ projects
thank you

You can try this rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(projects/|index\.php) [NC]
RewriteRule ^(.+)$ /projects/$1 [L]

Related

.htaccess rewrite cond/rule and hide url paremeters

I got a htaccess in my Root directory.
My files path http://example.com/folder1/folder2/index.php?country=eu&lang=en
I made this in htaccess.
RewriteCond %{HTTP_HOST} ^(www.)?anothersite.com$
RewriteRule ^([^/]*)/([^/]*)$ /folder1/folder2/index.php?country=$1&lang=$2 [L]
RewriteRule ^([^/]*)$ /folder1/folder2/index.php [L]
And looks like this
http://example.com/index.php?country=eu&lang=en
But i want like this -> http://example.com/eu-en
Is there a way to do this in htaccess?
EDIT:
I have to use rewrite cond, because im redirecting to folders depends of host.
EDIT 2: SOLUTION
RewriteCond %{HTTP_HOST} ^(www.)?anothersite.com$
RewriteRule ^([a-z]{2,2})-([a-zA-Z0-9_-]+)$ /folder1/folder2/index.php?country=$1&lang=$2 [QSA]
Thanks to answers, it is working now. But after that i got the css and js errors on website. Then i changed css and js paths /folder1/folder2/css.
You need to append with the [QSA] (query string append) tag. Try
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?lang=$1&page=$2 [QSA]
See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
Try with below,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)-([^/]+)$ index.php?country=$1&lang=$2 [QSA]

using htaccess in subfolders to redirects

i'm running a site with 2 subfolders (different sites in each i.e. I have 3 different sites in one host) so my question is that,
can I use .htaccess file in the root of subfolders? will it work? or I have to do all from my main site root and .htaccess?
would you please suggest a common code for my sobfolders to redirect to www versions with .ir prefix and remove index.php?
i want this:
mysite.ir/news [or] mysite.com/news [or]
to
www.mysite.ir/news
and also to remove index.php:
mysite.ir/news/index.php?/title [or] mysite.com/news/index.php?/title
to
www.mysite.ir/news/title
Thank you guys, any would be much appreciated
In the root folder of mysite.com. Create a .htaccess file with the below rules
RewriteEngine On
RewriteRule ^(.*)$ http://www.mysite.ir/$1 [R=301,L]
In the root folder of mysite.ir. Create a .htaccess file with the below rules
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.mysite.ir/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)/$ /$1/index.php?/$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)$ /$1/index.php?/$2 [L]

.htaccess Redirect main domain to subfolder

I am using one account for multi sites and I am wanting to have a clean public_html dir so I am trying to create my .htaccess to redirect the main domain to the subfolder.
I currently have as my result http://domain.co.nz/subfolder.
How can I work it so it just shows the original domain and also for www/http?
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.co.nz$
RewriteRule ^(/)?$ subfolder [L]
You need to adjust your code a little.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.nz$
RewriteCond %{REQUEST_URI} !^/subfolder [NC]
RewriteRule ^(.*)$ /subfolder/$1 [L]

Endless loop in .htaccess when rewrite to a folder

I've got the root of my domain with some folders.
app
scripts
shop
And I want to redirect with the .htaccess everything to the folder shop, except app and scripts. I wrote the next .htaccess, but I've got a 500 ERROR INTERNAL SERVER. I saw the log file and it says that there is and endless loop.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/app
RewriteCond %{REQUEST_URI} !^/scripts
RewriteRule (.*)$ shop/$1 [L]
I have another hosting with other apps and cms in the same hosting provider. Even with the same structure and it works perfect, then I don't undertand why doesn't work here.
How can I avoid the endless loop?
EDIT: I want that the redirect will be invisible, not with [R]
EDIT 2: My purpose is:
Having this folders:
app
scripts
shop
Access to the shop folder with the URL: domain.com
Access to the app folder with: domain.com/app
Access to the scripts folder with: domain.com/scripts
You'll also have to exclude URLs beginning with shop:
RewriteEngine On
RewriteRule ^/?$ /shop/ [L]
RewriteCond %{THE_REQUEST} ^GET\ /shop(/\S*)? [NC]
RewriteRule ^shop / [R=301,L,NC]
RewriteCond %{REQUEST_URI} !^/(app|scripts|shop)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /shop/$1 [L]

Rewriting ends up with not loading js / css

With the following .htaccess file, I obviously, get alot of 404's on loading scripts and css because the browser keeps looking in the wrong directory.
I am a newb at htaccess and have no clue about how to fix it.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^$ client/app/index\.html [L]
</IfModule>
Dir structure is as follows:
project /client / app / index.html
.htaccess is located in project directory.
I recomend you that redirect users with a 301 to correct path. This will end with your problems.
RewriteRule ^$ client/app/index\.html [L, 301]
If you are worried about seo look at https://support.google.com/webmasters/answer/93633?hl=en
All of the calls at your server are being redirected to your RewriteRule. Add a RewriteCond (RewriteCondition) to ignore CSS and JS files, if they exist:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^$ client/app/index\.html [L]
Explanation:
RewriteCond %{REQUEST_FILENAME} !-f
If the requested filename exists (css/main.css), do not go through with the RewriteRules.
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
If the file does not exist, but matches one of the extensions, do nothing. This is helpful because you get a 404 if css/main-2.css does not exist, rather than redirecting you to index.php.
And then finally is your own rule, which redirects all non-conditioned rules.
In your .htaccess file, this is what worked for me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/index.html
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^ /index.html [R=301]
Thank you!