using htaccess in subfolders to redirects - apache

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]

Related

.htaccess redirect issue with multiple .htaccess file

I am using .htaccess file like below for redirect www to non www on my public_html directory
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
its working fine and redirect like below
www.example.com
to
https://example.com
Now in my sub directory called latest, I have another .htaccess file like below
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
which is making my url clean like below
www.example.com/latest/index.php?page=1
to
www.example.com/latest/1
but even I have .htaccess file in home directory which is making www to non www, my this directory does not redirect www to non www.
I want redirect
www.example.com/latest/1
to
https://example.com/latest/1
I am sure .htaccess file in my directory is causing issue but I do not know how to resolve it. Let me know if anyone here can help me for the same.
Thanks!
The problem is, mod_rewrite is not inherited (at least by default). If you have a rewrite rule in the "default" directory .. You're going to have to include that rule in all subsequent directories "below" that. I have never seen (or heard of) a setting for allowing mod_rewrite to allow inheritance from parent .htaccess files. In general, this is just accepted as "the way it is".
UPDATE
THIS QUESTION In Server Fault explains this as well. The mod_rewrite rules from the previous htaccess files don't even get processed.
COMBINE THE 2 in the SUB DIRECTORIES
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# add this only to sub directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

.htaccess conflictions on different directories

Probably a duplicate question but here goes!
I'm not great at htaccess stuff. I've been using codeigniter and have multiple projects going on currently. I've made a redirect in my public_html to redirect to a directory called "home" right? And in this direct is the base for a codeigniter application where I have another .htaccess file removing the index.php from the url for controllers etc.
My question is are these two files conflicting one another? Here's my code:
public_html/.htaccess
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^myurl.co.uk$
RewriteRule (.*) http://www.myurl/home/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.myurl.co.uk$
RewriteRule (.*) http://www.myurl/home/$1 [R=301,L]
/home/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Also another question would be could I display my /home/index.php file on landing on myurl.co.uk without having home in the url?
In my public_html directory htaccess files I have
RewriteEngine on
RewriteCond %{HTTP_HOST} ^myurl.co.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.myurl.co.uk$
RewriteCond %{REQUEST_URI} !home/
RewriteRule (.*) /home/index.php/$1 [L]
And I changed my base url to http://www.myurl.co.uk/ whereas before I had http://www.myurl.co.uk/home. Ma bad!

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

Rewriting folder won't work

I have an app stored on my server in "SERVER_ROOT/app/1.0.1/index.php". The app is accessed by typing in "example.com/index.php" and I use a rewrite to achieve this.
The issue I am having is I want users to access "SERVER_ROOT/app/1.0.1/image/" by typing "example.com/image/" but none of my rewrites work when I edit the .htaccess file.
Here is my .htaccess file:
Options -MultiViews
RewriteEngine on
RewriteBase /app/1.0.1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+) index.php/$1 [L]
RewriteRule ^image/$ app/1.0.1/image/ [NC,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Thanks,
Peter
Since the /image/ directory doesn't actually exist, it'll get trapped by the first rule that routes everything to index.php. Assuming your htaccess file is in your document root, you need to add the rule above your routing rule and change it to this:
RewriteRule ^image/(.*)$ image/$1 [L]
If your htaccess file isn't in the document root, then you need to add this rule to the top of the htaccess file in your document root:
RewriteRule ^image/(.*)$ /app/1.0.1/image/$1 [L]

htaccess remove folder from url only

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]