apache apply rewrite rules to only one subdirectory - apache

I have the following directory structure:
/var/www/html
--myapp
--phpmyadmin
In the /var/www/html folder there is a .htaccess file with following content:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.sk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.mydomain.sk$
RewriteCond %{REQUEST_URI} !myapp/public
RewriteRule (.*) /myapp/public/$1 [L]
If I access the http://mydomain.sk the rewrite rules work as expected and I get the myapp/public/index.php file as root. However, if I want to access http://mydomain.sk/phpmyadmin the request is redirected to http://mydomain/myapp/public/phpmyadmin. Here I get the error 404 - page not found.
I cannot find a way to only serve http://mydomain.sk/myapp/public as a root folder and to serve http://mydomain.sk/phpmyadmin "as is".

It took some time to figure this one out. The rules posted above actually work as expected. It turns out I had another set of rewrite rules in apache2.conf file (that I totally forgot about) that took precedence over my .htaccess file.

Related

htaccess not working but check successful localhost

I have a simple .htaccessfile
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^v4r.info/(.*)/(.*) v4r.info/NGOplus/index.php?NGO=$1&page=$2 [L,QSA]
I tested the file in htaccess.madewithlove.com, it gives a correct result and copy&pasting the result works flawlessly. (http://localhost/v4r.info/NGOplus/index.php?NGO=action-for-woman&page=board.list.php&ff=710;;;;;&startdate=2017-11-11)
But htaccess fails on localhost with an error:
File does not exist:
/var/www/html/public_html/v4r.info/action-for-woman/board.list.php
The test URL is
localhost/v4r.info/NGOplus/index.php?NGO=action-for-woman&page=board.list.php&ff=710;;;;;&startdate=2017-11-11
htaccess is active. (rubbish line gives "internal server error")
in another directory htaccess is working fine.
apache.conf seems ok (AllowOverride All)
Added:
The htaccess file is not in the base directory but in the 1. subdirectory (v4r.info).
What works is htaccess in v4r.info/NGOplus with a symlink 'action-for-woman' to NGOplus
RewriteRule ^(.+?)/?$ index.php?page=$1 [L,QSA]
Here, apache does a «local» rewrite, i.e. just the last part of the URL (the directory name 'action-for-woman' I have to extract from $_SERVER ...)
my .htaccess file is in v4r.info directory what is not the root directory.
In that case, your rule will never match. The RewriteRule pattern matches a URL-path relative to the directory that contains the .htaccess file.
But anyhow, rewriting is not recursive afaik.
Yes, it is "recursive" in a directory context (ie. .htaccess). In that the rewrite engine "loops" repeatedly until the URL passes through unchanged, or you have explicitly set END (Apache 2.4).
Try the following instead:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !index\.php$
RewriteRule ^([^/]+)/([^/]+)$ /v4r.info/NGOplus/index.php?NGO=$1&page=$2 [L,QSA]
The check against the REDIRECT_STATUS environment variable is to ensure that only direct requests are rewritten and not already rewritten requests.
However, this pattern is still far too generic as it matches any two path segments. I put the 2nd condition that checks index.php just so you can request /v4r.info/NGOplus/index.php directly (as you were doing in your tests). However, this could be avoided by making the regex more specific.

Apache mod_rewrite fake pushState directory mapping

If have played around a lot with mod_rewrite rules in my httpd.conf file. Regardless of my research i haven't been able to get a couple of things working.
This is my file structure:
/
-index.php
-app.php
/css
-style.css
/js
-script.js
The server should either serve the index.php (home page) or app.php (single application page). Both the script and style files are included in both php files.
Goals
My domain domain.com should serve the index.php, the address bar should show www.domain.com
(This seems to work already, per default.)
The subdomain domain.com/a should be changed to domain.com/a/ if necessary. This domain should serve the app.php file.
(This is already working to an extend. One problem is that the relative links inside app.php are wrong, because the file "thinks" it is in a subdirectory instead of root. This I would like to change)
Anything after domain.com/a/ e.g. domain.com/a/user/10 should stay in the address bar and serve the app.php as usual. Ideally, to preserve relative links again, the file should "know" it is in the root folder.
(This is in order to support a "fake" pushState server) EDIT Clarification: Everything after the /a/ will be interpreted by my Javascript app. When the client clicks a link like domain.com/a/user/10 there will be no extra request to the server.
Bonus
Add trailing slashes to all URLs except the root url.
e.g. turn domain.com/a/user/10 into domain.com/a/user/10/
Add www to URL in case it is missing.
What I've tried
-add www
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]
-add slashes
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301]
-redirect /a/
RewriteCond %{REQUEST_URI} ^(/a/)$
RewriteRule ^ /app.php
Any pointers or help are greatly appreciated! Thanks.
Edit
I have used this tool http://htaccess.madewithlove.be/ to test some conditions.

htaccess adding WWW and changing filename in subdirectory

I know similar questions have come up, though often without a working answer. I'm hoping to have better luck!
I have an .htaccess file in my root directory adding "www" to everything:
RewriteEngine on
RewriteCond %{SERVER_NAME} ^mysite.org
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=permanent,L]
This generally works fine. I have a subfolder (/myquiz/) in which the old index.html file has been replaced with index.php. I know there are external links to /myquiz/index.html, so I want to make sure those redirect. Leaving index.html in place and trying to redirect from that led to some odd behavior, but adding an .htaccess in that directory works for that:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html?$ /myquiz/index\.php [NC,R]
Trying to load index.html redirects to index.php as hoped for, and the WWW gets added if needed. But requesting mysite.org/myquiz/index.php directly does not add the WWW.
I tried adding "RewriteEngine inherit", but that resulted in calls getting redirected to my root folder instead. A great trick if I want to make a subfolder inaccessible, but not helping here. I also tried just adding the code from my root .htaccess into the beginning of my subfolder's .htaccess, but that worked no better.
Any ideas?
You shouldn't need to add another htaccess file in the myquiz folder. This should work in the htaccess file in the root of the site. Remove the htaccess file in myquiz and try this.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.org
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=301,L]
RewriteRule ^myquiz/index\.html$ /myquiz/index.php [R=301,L]
Also I wouldn't use %{SERVER_NAME} unless your are sure the name is set properly in the config file. Then it can be more reliable than HTTP_HOST, otherwise I would instead use %{HTTP_HOST}.
I think inherit would work if you add an L flag to the rule that you have in your myquiz folder:
RewriteRule ^index\.html?$ /myquiz/index\.php [NC,R,L]
So that it redirects first, then the inherited rule (the www) gets applied after.
You could also just put both rules in the same file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.org$ [NC]
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=permanent,L]
RewriteRule ^myquiz/index\.html$ http://www.mysite.org/myquiz/index.php [R=permanent,L]

.htaccess redirect based on multiple conditions

Im trying to configure a .htaccess RewriteRule and i'm struggling.
I have a domain www.domain.com
I'm trying to add these conditions.
I'm working on www.domain.com/.htaccess
if URL is www.domain.com with no path to file, redirect to www.domain.co.uk
if URL is www.domain.com AND has a path to file, no rewrite, allow.
if URL is www.domain.com AND has a path to file AND file doesnt exist, return 404
if URL points to a directory, return access forbidden
They are my requirments.
I've tried multiple things. Heres my .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,L]
This code satisfies the first constraint.
However i have no idea how to check if a path exists, if the path is valid, or if its a directory.
A perfect answer would be code satisfying all the constraints and explainatoin how it works.
I've read the mod_rewrite documentation more times than I care to admit :'(
# Make sure / redirected to other domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^/$ http://www.domain.co.uk [R=301,L]
# Make sure directory listings are forbidden
Options -Indexes
Note that you can use RedirectMatch, which is actually preferred over rewriting in this case
and there is no need to handle files at all in your case - if the URI is not /, then the rules do not apply and regular file handling will be performed (404 error if not found, etc... )

.htaccess Apache on cPanel only working in some cases

I'm using the Zend Framework, so I'm bootstrapping into a file called index.php. Naturally, I don't want images to be bootstrapped, so I've added a .htaccess file. Here's what it looks like
/application
/library
/public (this is the root of the site)
/images
/js
.htaccess
index.php
This is what's written in my .htaccess:
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|mp3|mov|css)$ index.php
This is basically saying if it doesn't end in .js / .ico / etc, then send the request to index.php. It works just fine on my localhost, but when I get up to actually putting it online, it doesn't. It just routes everything to index.php, regardless of the ending of the request. When mywebsite.com/images/wizard.gif should just show the picture, it tries to load the images controller, which is not what I want it to do.
What could be going wrong? I know it's reading the .htaccess. Is it reading my regex wrong? Why would one apache server read it wrong, while another reads it correctly? Any help would be great.
Here is my .htaccess if you would like to give it a try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
Basically it checks to make sure the request is not a directory, symlink or a real file, and then sends it to index.php. Otherwise it will provide direct access to the file/directory