.htaccess check if rewrite subdirectory exists - apache

I want to point each subdomain to a corresponding directory by default. To make things simple subdomain and directoryname are the same. This works well but is there any way to check if the actual directory exists? If not I don't want to do the rewrite.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$
RewriteCond %{REQUEST_URI} !^/%1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$
RewriteRule ^(/)?$ %1/index.php [L]

You may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteCond %{DOCUMENT_ROOT}/%1/index.php -f
RewriteRule ^ %1/index.php [L]
This will rewrite to a subdomain.example.com/foobar to subdomain.example.com/subdomain/index.php only if subdomain directory exists with a index.php file in it.

Related

Long URL in php using htaccess

I have a very long URL: //domain/administration/registrar/term_detail.php?a_id=47
And I want to make it looks like: //domain/administration/registrar/detail/47
I have created an htaccess file in //domain/administration/registrar/. Below is my code in htaccess and it is not working. Any suggestion of what I did wrong is appreciated.
Both of my htaccess and term_detail.php files are inside of registrar folder.
RewriteEngine On
RewriteBase /administration/registrar/
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \s/+registrar/detail\.php\?a_id=([^\s&]+) [NC]
RewriteRule ^ administration/registrar/%1? [R=301,L]
RewriteRule ^administration/registrar/([a-zA-Z0-9]+)/?$ detail?a_id=$1 [L,QSA,NC]
You can try these rules inside your /administration/registrar/.htaccess:
RewriteEngine On
RewriteBase /administration/registrar/
## External redirect to /administration/registrar/term_detail/47
RewriteCond %{THE_REQUEST} /([^./]+)(?:\.php)?\?a_id=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L,NE]
## Internal rewrite to term_detail.php?a_id=47
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?a_id=$2 [QSA,L]
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
Keep your htaccess file inside registrar folder.
RewriteEngine ON
##External redirect to //domain/administration/registrar/detail/47 URL.
RewriteBase /administration/registrar/
RewriteCond %{THE_REQUEST} \s/([^/]*)/([^/]*)/(?:[^_]*)_([^.]*)\.php\?a_id=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L]
##Internal rewrite to term_detail.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ $1/$2/term_$3.php?a_id=$4 [QSA,L]

htaccess multiple RewriteConds

This is my site root
/public_html
beta/
.htaccess
public/
index.php
.htaccess
The first .htaccess inside beta folder is for changing dir from mysite.com/beta to mysite.com/beta/public:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.mysite.com/$
RewriteCond %{REQUEST_URI} !beta/public/
RewriteRule (.*) /beta/public/$1 [L]
The other.htaccess inside public folder is for shorten url from index.php?photo_id=id to /id:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?photo_id=$1
How can I make these work both since for now only one of them work not together? Thanks
Why do you have "non public" stuff in public_html? If you want it private, shouldn't you place it in a private directory?
For the beta htaccess file, try removing the absolute URLs
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\)?mysite\.com$ [NC]
RewriteCond $1 !^public
RewriteRule ^(.*)$ public/$1 [L]
Note that the trailing slash needs to be removed from the HTTP_HOST, there's not slashes in that variable.
For the public folder, also remove the absolute URLs
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?photo_id=$1

Mod_rewrite without {%Document_Root}

I used to point all my domains to my web-spaces root. And use an htaccess file like this to point the different domains into different folders
RewriteCond %{HTTP_HOST} ^new.vea.?re.?[net|de]?(.*)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/veare/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/veare/$1 -d
RewriteRule ^(.*)$ veare/$1 [L]
# rewrite parameters that are neither files nor folders
RewriteCond %{HTTP_HOST} ^new.vea.?re.?[net|de]?(.*)?$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ veare/index.php?/$1 [L]
My problem now is, that my new web provider does not set the %{DOCUMENT_ROOT} so this script doesn't work. Is there any workaround?
So its like this.
www.test.com/css/style.css
1. Domain points to root.
2. If root/test.com/css/style.css exists, it needs to just grab this file, aka redirect into the subfolder test.com
3. If it is neither a file, nor a folder, it should just point to the index.php in the subfolder root/test.com/index.php
How can i solve it WITHOUT %{DOCUMENT_ROOT}?
Okay,
easy fix, use the <?php phpinfo(); ?> which should indeed tell you the real document root. eg. customer/123445_12345. Now you can just use this in your htaccess. Note, that I also had to use %{REQUEST_URI} instead of %{REQUEST_FILENAME} to get the full path, like /css/app.css
RewriteCond %{HTTP_HOST} ^test.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond /customer/123445_12345/test/public/%{REQUEST_URI} !-f
RewriteCond /customer/123445_12345/test/public/%{REQUEST_URI} !-d
RewriteRule ^(.*)$ /formandsystem/cms/test/index.php?/%{REQUEST_URI} [L]
RewriteCond /customer/123445_12345/test/public/%{REQUEST_URI} -f [OR]
RewriteCond /customer/123445_12345/test/public/%{REQUEST_URI} -d
RewriteRule ^(.*)$ /test/public/%{REQUEST_URI} [L]

mod_rewrite rules in .htaccess

I want to do change my document root using rewrite rules in my .htaccess file, and also redirect all requests to index.php except static resources
In other words:
if a file with the path prepended with www exists (i.e. www/{%request_uri}):
http://domain.com/css/main.css should be rewritten to http://domain.com/www/css/main.css
if that file does not exist, redirect to the app main entry point www/index.php:
e.g.
http://domain.com should be rewritten to http://domain.com/www/index.php
http://domain.com/hello should be rewritten to http://domain.com/www/index.php/hello
http://domain.com/hello?a=1 should be rewritten to http://domain.com/www/index.php/hello?a=1
I tried variations, however either it gives us 500 internal server error, or infinite loop. Here is one that gives 500 internal server error:
# try to find it in www/
RewriteCond {%DOCUMENT_ROOT}/www%{REQUEST_URI} -f
RewriteCond {%DOCUMENT_ROOT}/www%{REQUEST_URI} -d
RewriteRule ^(.+) www/$1 [L]
RewriteRule ^(.*) www/index.php/$1 [L]
This one also:
RewriteCond %{REQUEST_URI} !www/
RewriteRule (.*) /www/$1
RewriteCond {%REQUEST_FILENAME} !-f
RewriteCond {%REQUEST_FILENAME} !-d
RewriteRule www/(.*) /www/index.php/$1 [PT,L]
Can anyone help?
I think that's impossible with a single .htaccess but it's possible with two.
You need one in your root directory, /.htaccess:
RewriteEngine on
RewriteBase /
# We don't want infinite rewriting.
RewriteCond %{REQUEST_URI} !www/.*$
RewriteRule ^(.*)$ www/$1 [L]
And another in your www directory, /www/.htaccess:
RewriteEngine on
RewriteBase /www
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Well actually I made a stupid mistake! The % sign should be outside of the {} bracket!
This can be done in one .htaccess in root dir:
RewriteEngine on
RewriteCond %{REQUEST_URI} !www/
RewriteRule ^(.*)$ www/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^www/(.*)$ www/index.php/$1 [L]

How can I disable RewriteRule for one subcategory?

I have a .htaccess in my root of website that looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.pl [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([a-z0-9_-]+)\.mydomain\.pl [NC]
RewriteRule ^/?$ /index.php?run=places/%1 [L,QSA]
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !^/upload/
RewriteCond %{REQUEST_URI} !^/javascript/
RewriteRule ^(.*)$ /index.php?runit=$1 [L,QSA]
But when I type:
mydomain.pl/guests
I would like to go normally to actual folder guests. I understand that I need to somehow disable this rule for guests subfolder, but how do I do this?
EDIT:
I've included whole .htaccess file
I've put a htaccess in subfolder and added RewriteEngine Off and it works.
Add one of these
RewriteEngine On
#If the request starts with guests, then pass it through
RewriteCond %{REQUEST_URI} ^guests
RewriteRule ^ - [L,QSA]
#Or, if the request contains an existing filename or directory, pass it through
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L,QSA]