htaccess subdomain with subfolders access - php

I am trying to rewrite the subdomain request to a subfolder in my server using .htaccess. I want mail.domain.com to look into mail folder located in the root. I am able to achieve this with the below code
RewriteEngine on
RewriteCond %{HTTP_HOST} mail.domain.com$
RewriteCond %{REQUEST_URI} !^/mail
RewriteRule ^(.*)$ /mail/$1 [L]
This WORKS correctly. When i browse to mail.domain.com i am getting the contents of domain.com/mail/index.php.
However this doesn't work with the subfolders inside the subdomain. ie when i browse to mail.domain.com/installer it DOESN'T give the contents from domain.com/mail/installer/index.php. Instead it shows 404 error. I also tried the code from htaccess subdomain pointing . Somehow I am unable to get the output. That method also gives the same problem. What am I doing wrong?
Note:
I do not want redirect conditions. Want to achieve via rewrite rules.
I am using openshift server. created domain.com & mail.domain.com aliases.
Root folder contains wordpress with no below .htaccess rules.
Edit
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
When I remove the above wordpress htaccess all works well. So it is causing 404 error in subdomain subfolders. Please help what i can do for both wordpress pretty urls and subdomain subfolders to work?
Note: I am really sorry. Wordpress added this via web interface and i failed to notice that.

Keep your root .htaccess like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} =mail.domain.com
RewriteRule ^((?!mail).*)$ mail/$1 [L,NC]
RewriteRule ^(index\.php$|mail) - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Related

Yii2: Hiding frontend/web and backend/web from UrlManger

I managed to hide frontend/web and backend/web/ using .htaccess file. But the urlManager unable to understand that. I'm trying to create url like site/index and it should be something like this: example.com/site/index but it creates example.com/frontend/web/site/index.
How do I solve this problem with UrlManager?
Update: .htaccess in root directory.
Options -Indexes
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} !^/backend/web/(assets|css)/
RewriteCond %{REQUEST_URI} admin
RewriteRule .* backend/web/index.php [L]
RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css)/
RewriteCond %{REQUEST_URI} !admin
RewriteRule .* frontend/web/index.php [L]
</IfModule>
and here is the one I used in both frontend/web and backend/web:
RewriteEngine on
# if a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward the request to index.php
RewriteRule . index.php
I use this .htaccess config and it works fine for me. You need only one .htaccess file in your site root directory, and set frontend / backend configuration as described. Maybe this will work for you too.

.htaccess Root Rewrite Without Folders

I've spent countless hours searching for similar issues and found the solutions to not work for me. I have an index.php with an 'id' GET variable to load pages dynamically as mydomain.com/[idVariableHere].
My .htaccess code works:
RewriteEngine On
RewriteBase /
RewriteRule ^(js|css|images)($|/) - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?id=$1 [L,QSA]
However, it's not preventing the /js/, /css/, and /images/ folders from being displayed. It's interfering with the last RewriteRule and I have no idea how to get around this.
My goal is to keep the mydomain.com/[idVariableHere] look with restriction on these important folders. Please help.
Have it this way:
ErrorDocument 404 default
ErrorDocument 403 default
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?id=$1 [L,QSA]
From what I understand, you want an .htaccess to prevent access to /js/, /css/, and /images/ folders. Assuming these are found in the root. Just create an .htaccess file in each folder with the following code:
deny from all
This was taken from:
deny direct access to a folder and file by htaccess

Rewriting all request to index.php in the same diectory

I want all the request to be rewrited to index.php so I used this htaccess code.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
It works fine when site is hosted on home directory. But suppose if I move my site to a subdirectory xyz and move all my files including .htacess file inside this directory. Then if I access http://example.com/xyz/some_page, the request is not redirected to /xyz/index.php.
So, How can I make this rewrite work even on subdirectories as in my case.
Update:
I forgot to mention the directory xyz as in my case is likely to change frequently. So, this directory doesn't need to be hard coded in the rewrite rule
Remove the RewriteBase
If you don't need the rewrite base - just remove it. A working example based on the one shown in the question would be:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Note that the first rewrite rule has been deleted as it is logically equivalent to RewriteCond %{REQUEST_FILENAME} !-f (do not rewrite requests for a file that exists).
Consider also deleting RewriteCond %{REQUEST_FILENAME} !-d as this prevents urls like /this/folder/exists being sent to index.php. Most relevant if Directory listings are enabled but not desired.
change your base url for rewrite
RewriteBase subdir/
or try with full url
RewriteBase http://www.examle.com/subdir/

htaccess rules for website are not working properly

I have website where on root i have htacess files and two folder for (blog->WP,shop->Magento).
My directory structure are like this root (htaccess,other files,blog,shop).this was my htaccess code on root
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ "http\:\/\/www\.example\.com\/$1" [R=301,L]
It works with these url's like example.com and example.com/blog ->for WP and example.com/shop-> for Magento.But now i transferred to new domain and convert site to ip base.I want to access my site like 192.168.2.36.If i change above htaccess code with ip it only works for home, for inner it gives errors.Please help how can i solve the issue.I want to access my site like IP/blog,IP/shop
Add this in your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /shop/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /shop/index.php [L]
</IfModule>

.htaccess IP Blocking not working

I can't seem to get this .htaccess IP blocking script to work. I want to block any IP except mine from being able to access the wordpress login script and admin folder. My IP is 55.55.555.55 in this code. It works in that it blocks access to the file and folder from all IP's, but it doesn't let me access it through my IP. I get the 403 error as well. I am running Cloudflare, I don't know if that could be causing problems. Here is the current .htaccess
RewriteEngine On
RewriteBase /
#Block everyone but me
RewriteCond %{REMOTE_ADDR} !^55\.55\.555\.55$
RewriteRule ^(wp-login\.php|wp-admin) - [F,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I don't know why it isn't working, I've tried half a dozen variations of the wordpress login .htaccess IP blocker, and the IP is my actual IP, the IP it showed on 5 different 'What's my IP' sites. When I delete or rename the .htaccess, anyone can view the login page, but when it's there I can't either.
Also, the blog itself is in blog.domain.com, domain.com does a 301 redirect to take it there, so the blog and .htaccess are in blog.domain.com/. I doubt that makes a difference, but that's how it's set up.
Update: I tried using the same .htaccess on another site I have, which doesn't have cloudflare, here is the .htaccess I used
AddHandler php-stable .php
RewriteEngine On
RewriteBase /
#Block everyone but me
RewriteCond %{REMOTE_ADDR} !^55\.55\.555\.55$
RewriteRule ^(index.php) - [F,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
As you can see, I changed it to only work on the index.php, and I made a quick test index.php page. When I went there on my computer, it loads it, but proxies return the 403 correctly. Could cloudflare somehow be causing the problems? Even though I disabled the caching, could it be screwing with it?
I just found this https://www.cloudflare.com/resources-downloads#mod_cloudflare I'm reading it now
Order of rules in .htaccess is very important. In your case WP rules are above and taking full control.
Change order or the rules:
RewriteEngine On
RewriteBase /
# Block everyone but me
RewriteCond %{REMOTE_ADDR} !^55\.55\.555\.55$
RewriteCond %{THE_REQUEST} \s/+(wp-login\.php|wp-admin/)[\s?] [NC]
RewriteRule ^ - [F]
# regular WP stuff
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Categories