RewriteCond -d .htaccess failing but directory exists - apache

I am trying to conditionally rewrite the directory path of a URI if an override exists and the override directory exists in an Apache 2.2 .htaccess file running on RHEL5.
I am doing this by conditionally setting an environment variable to a directory and then later I am checking if the environment variable is set and the directory exists. This is for a Drupal site but not sure that matters.
RewriteCond %{HTTP_HOST} qa-public.sample.com
RewriteRule ^ - [ENV=publicFiles:sites/qa-public.sample.com/files]
...
RewriteCond %{ENV:publicFiles} !^$
RewriteCond %{ENV:publicFiles} -d
RewriteRule ^sites/default/files(/.*)$ %{ENV:publicFiles}$1 [PT]
I turned on rewrite logging to level 7.
10.20.67.192 - - [27/Feb/2014:13:16:34 --0500] [qa-public.sample.com/sid#7f631331ecd8][rid#7f631360e568/initial] (4) [perdir /var/www/qa-public/] RewriteCond: input='sites/qa-public.sample.com' pattern='!^$' => matched
10.20.67.192 - - [27/Feb/2014:13:16:34 --0500] [qa-public.sample.com/sid#7f631331ecd8][rid#7f631360e568/initial] (4) [perdir /var/www/qa-public/] RewriteCond: input='sites/qa-public.sample.com' pattern='-d' => not-matched
I used the output of perdir and input to verify it exists. It is a symbolic link pointing to a directory.
$ ls -l /var/www/qa-public/sites/qa-public.sample.com/files
lrwxrwxrwx 1 XXXXXX YYYYYY 27 Feb 13 14:20 /var/www/qa-public/sites/qa-public.sample.com/files -> /usr/local/files-qa/
I have also tried -l and -x. I have also tried other non-linked directories.
I have tried adding a / before and/or after the directory in the RewriteCond.
I can remove the -d condition and Apache will serve the file.
Thanks,
Wes.

Try this condition:
RewriteCond %{DOCUMENT_ROOT}/%{ENV:publicFiles} -d

Related

Install Symfony 4 in a subfolder of DocumentRoot

I hope that this question is not a duplicate, but I only found articles or questions about this for old versions of Symfony. I am new to Symfony 4 and I would like to create a new application with it and install it as a part of an already existing website (domain below). The Sf 4 app goes into the admin/ subdirectory shown in the hierarchy below. domain uses the TYPO3 CMS, but I’m not sure that it matters for this question.
First, here is the directory structure :
/home/webuser/domain/
public/ (this is the document root for "domain.localhost")
admin/ (the Symfony app goes here)
public/
…
…
typo3/
.htaccess
index.php
…
composer.json
I am currently working on the website on my local machine and I would like to access the Symfony application root from domain.localhost/admin. I am using an Apache virtual host and the domain/public folder is the document root of domain :
<VirtualHost *:80>
ServerName domain.localhost
DocumentRoot "/home/webuser/www/domain/public"
</VirtualHost>
I tried adding the following rewrite rule to my VirtualHost :
<VirtualHost *:80>
ServerName domain.localhost
DocumentRoot "/home/webuser/www/domain/public"
RewriteEngine On
RewriteRule "^/admin/(.*)$" "/admin/public/$1"
LogLevel trace1
</VirtualHost>
But this seems to work only when I visit http://domain.localhost/admin/ : I then see the "Welcome to Symfony 4.2.7" page. As soon as I try to visit a subpage, I get a 404 error. I tried to create my first page at lucky/number as explained in the documentation here, and looking at the Apache error log, I have lines such as :
[Mon Apr 29 18:52:09.472043 2019] [rewrite:trace1] [pid 22250:tid 140404453660416] mod_rewrite.c(483): [client ::1:43084] ::1 - - [domain.localhost/sid#55a33c704bf8][rid#7fb260002bd0/initial] [perdir /home/webuser/www/domain/public/] pass through /home/webuser/www/domain/public/admin/public/lucky
[Mon Apr 29 18:52:09.472087 2019] [core:info] [pid 22250:tid 140404453660416] [client ::1:43084] AH00128: File does not exist: /home/webuser/www/domain/public/admin/public/lucky/number
Now, TYPO3 has an .htaccess file containing URL rewrites, but I don’t think that they are interfering. Below, I just added myself the admin/ folder in the rule after the comment that starts with "Stop rewrite processing", so that we are not redirected to the home page of the domain website when the file is not found :
<IfModule mod_rewrite.c>
# Enable URL rewriting
RewriteEngine On
# Store the current location in an environment variable CWD to use
# mod_rewrite in .htaccess files without knowing the RewriteBase
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=CWD:%2]
# Rule for versioned static files, configured through:
# - $GLOBALS['TYPO3_CONF_VARS']['BE']['versionNumberInFilename']
# - $GLOBALS['TYPO3_CONF_VARS']['FE']['versionNumberInFilename']
# IMPORTANT: This rule has to be the very first RewriteCond in order to work!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ %{ENV:CWD}$1.$3 [L]
# Access block for folders
RewriteRule _(?:recycler|temp)_/ - [F]
RewriteRule fileadmin/templates/.*\.(?:txt|ts)$ - [F]
RewriteRule ^(?:vendor|typo3_src|typo3temp/var) - [F]
RewriteRule (?:typo3conf/ext|typo3/sysext|typo3/ext)/[^/]+/(?:Configuration|Resources/Private|Tests?|Documentation|docs?)/ - [F]
# Block access to all hidden files and directories with the exception of
# the visible content from within the `/.well-known/` hidden directory (RFC 5785).
RewriteCond %{REQUEST_URI} "!(^|/)\.well-known/([^./]+./?)+$" [NC]
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule (?:^|/)\. - [F]
# Stop rewrite processing, if we are in the typo3/ directory or any other known directory
# NOTE: Add your additional local storages here
RewriteRule ^(?:typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico|admin/) - [L]
# If the file/symlink/directory does not exist => Redirect to index.php.
# For httpd.conf, you need to prefix each '%{REQUEST_FILENAME}' with '%{DOCUMENT_ROOT}'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ %{ENV:CWD}index.php [QSA,L]
</IfModule>
The production server will be a dedicated system, so I can modify anything I want.
It turns out that I just needed to install the Apache pack, like in this answer

mod_rewrite not finding files that exists

I am working on a project that uses Apache 2.2 and we are implementing a rewrite rule that uses the file on the local disk if it exists and if it doesn't then it using another server to server the file. To simplify the problem I have just put a rule in that redirects to a given URL if the file doesn't exist:
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(.*) /notfound.html
Now the output on that request from the rewrite log is as follows:
127.0.0.1 - - [30/May/2016:16:54:01 +1000] [www.blah.com/sid#7f8a4ee0f358][rid#7f8a4f45d7d8/initial] (2) init rewrite engine with requested uri /index.html
127.0.0.1 - - [30/May/2016:16:54:01 +1000] [www.blah.com/sid#7f8a4ee0f358][rid#7f8a4f45d7d8/initial] (3) applying pattern '^(.*)' to uri '/index.html'
127.0.0.1 - - [30/May/2016:16:54:01 +1000] [www.blah.com/sid#7f8a4ee0f358][rid#7f8a4f45d7d8/initial] (4) RewriteCond: input='/index.html' pattern='!-f' => matched
127.0.0.1 - - [30/May/2016:16:54:01 +1000] [www.blah.com/sid#7f8a4ee0f358][rid#7f8a4f45d7d8/initial] (2) rewrite '/index.html' -> '/notfound.html'
127.0.0.1 - - [30/May/2016:16:54:01 +1000] [www.blah.com/sid#7f8a4ee0f358][rid#7f8a4f45d7d8/initial] (2) local path result: /notfound.html
127.0.0.1 - - [30/May/2016:16:54:01 +1000] [www.blah.com/sid#7f8a4ee0f358][rid#7f8a4f45d7d8/initial] (2) prefixed with document_root to /var/www/html/notfound.html
127.0.0.1 - - [30/May/2016:16:54:01 +1000] [www.blah.com/sid#7f8a4ee0f358][rid#7f8a4f45d7d8/initial] (1) go-ahead with /var/www/html/notfound.html [OK]
When I turn off the rewrite rules and run the same command I get a 200 response from the server.
The server has permissions to view the file.
Any help would be appreciated.
Use %{REQUEST_FILENAME} instead of %{REQUEST_URI}, thus:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) /notfound.html
%{REQUEST_URI} will contain only /index.html, so it won't work unless the filesystem has an index.html file in the root.
%{REQUEST_FILENAME} will contain the full path to the requested filename (including the Apache directory root for that domain)
Note that REQUEST_URI and REQUEST_FILENAME might happen to be equal under some specific circumstances, but more often than not, they won't. So if you are checking for a filename in the server's local filesystem (as it's the case), use the specific.

Rewrite rules for CMS fail after installing ssl certificate

I wrote a content management system that uses rewrite rules to map urls to controller, action and argument query strings.
I use two .htaccess files. One is in my site's root directory. This one forwards all requests to a sub directory, which depends on the domain name used for the request:
SetEnv HTTP_MOD_REWRITE On
RewriteEngine on
# mapp requests that don't start with www to https://www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# BEGIN Domain to folder mapping
########################
# pointing example.com to subfolder 'example'
ReWriteCond %{HTTP_HOST} (www\.)?example.com
ReWriteCond %{REQUEST_URI} !example/
ReWriteRule ^(.*)$ example/$1 [L]
# END Domain to folder mapping
A second .htaccess file that maps the request to the actual query string is in the subdirectory that gets mapped to in the first rewrite step:
SetEnv HTTP_MOD_REWRITE On
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([^/]*)([/]*)([^/]*)([/]*)(.*) index.php?controller=$1&action=$3&args=$3 [L,QSA]
The result of all this, before I installed my ssl certificates, was that a request that looks like this:
example.com/hello/foo/123
would map to:
http://www.example.com?controller=hello&action=foo&args=123
Now I just get sent to the index file. If I enter the actual query string, I get the correct result.
I looked through my server's rewrite logs and it looks like there is an additional rewrite step that gets executed without me being able to figure out why. I think this is the relevant stuff from the rewrite logs:
[rewrite:trace3] [www.example.com/sid#8021c1788][rid#807418748/initial/redir#1] [perdir /fs6c/example/public/example/] strip per-dir prefix: /fs6c/example/public/example/hello -> hello
[rewrite:trace3] [www.example.com/sid#8021c1788][rid#807418748/initial/redir#1] [perdir /fs6c/example/public/example/] applying pattern '([^/]*)([/]*)([^/]*)([/]*)(.*)' to uri 'hello'
[rewrite:trace2] [www.example.com/sid#8021c1788][rid#807418748/initial/redir#1] [perdir /fs6c/example/public/example/] rewrite 'hello' -> 'index.php?controller=hello&action=&args='
[rewrite:trace3] [www.example.com/sid#8021c1788][rid#807418748/initial/redir#1] split uri=index.php?controller=hello&action=&args= -> uri=index.php, args=controller=hello&action=&args=
I am assuming the [L] flag is the culprit, but I don't understand why this wasn't an issue before I switched to ssl.
I am using a shared hosting service, and they had to install the certificates for me, since they don't expose the necessary parts of the server to the user.
I still don't know WHY this is happening, but I was able to find a way to make it work with the ssl certificate. These are the relevant rewrite rules in the htaccess file located in the root directory:
# for existing files, redirect to subdirectory
ReWriteCond %{HTTP_HOST} ^(www\.)?example.com
ReWriteCond %{REQUEST_URI} !example/
RewriteCond %{DOCUMENT_ROOT}/example/$1 -f
ReWriteRule ^(.*) example/$1 [L]
# for non-existing files, map to index.php with CMS arguments
ReWriteCond %{HTTP_HOST} ^(www\.)?example.com
# file exists, but is index.php
ReWriteCond %{REQUEST_URI} index.php [OR]
# file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
ReWriteRule ^([^/]*)([/]*)([^/]*)([/]*)(.*) example/index.php?controller=$1&action=$3&args=$5 [L,QSA]
I eliminated the second htaccess file.
I hope this helps someone in the future. The hosting provider is NearlyFreeSpeech. If anyone has any clue why my old rules didn't work, I would really appreciate to know!

Trouble with serving local content while using RewriteRule [P]

I'm trying to write rules that will serve files on the new server, while proxying files that don't exist, to the old server. The proxy rules work fine, sending missing files and directories to the old server.
The problem I'm having is new-server.example.com/blah (/blah/ exists on the new server) is passed through to the proxy rules and routed to the old server.
The reason for attempting the skip is because new-server.example.com/blah falls through to the "proxy section" and passes new-server.example.com/blah/index.php. Index.php doesn't exist on the new server, index.html does. The "proxy section" doesn't follow all the index file possibilities in DirectoryIndex, it just looks at the first one.
So I end up with error message, "The requested URL /blah/index.php was not found on this server."
How can this be written so it actually works? -- Thanks.
RewriteEngine On
# skip, proxy section
RewriteCond /var/www/html%{REQUEST_FILENAME} -f [OR]
RewriteCond /var/www/html%{REQUEST_FILENAME} -d
RewriteRule .? - [S=4]
# proxy section
RewriteCond /var/www/html%{REQUEST_FILENAME} !-f
RewriteCond /var/www/html%{REQUEST_FILENAME} !-d
RewriteRule ^/(.*) http://old-server.example.com/$1 [P]
ProxyPassReverse / http://old-server.example.com/
# rewrite log
pass through /index.php
init rewrite engine with requested uri /
applying pattern '.?' to uri '/'
RewriteCond: input='/var/www/html/' pattern='-f' => not-matched
RewriteCond: input='/var/www/html/' pattern='-d' => matched
pass through /
init rewrite engine with requested uri /index.php
applying pattern '.?' to uri '/index.php'
RewriteCond: input='/var/www/html/index.php' pattern='-f' => matched
pass through /index.php
init rewrite engine with requested uri /blah/
applying pattern '.?' to uri '/blah/'
RewriteCond: input='/var/www/html/blah/' pattern='-f' => not-matched
RewriteCond: input='/var/www/html/blah/' pattern='-d' => matched
pass through /blah/
init rewrite engine with requested uri /blah/index.php
applying pattern '.?' to uri '/blah/index.php'
RewriteCond: input='/var/www/html/blah/index.php' pattern='-f' => not-matched
RewriteCond: input='/var/www/html/blah/index.php' pattern='-d' => not-matched
applying pattern '^/(.*)' to uri '/blah/index.php'
RewriteCond: input='/var/www/html/blah/index.php' pattern='!-f' => matched
RewriteCond: input='/var/www/html/blah/index.php' pattern='!-d' => matched
rewrite '/blah/index.php' -> 'http://old-server.example.com/blah/index.php'
forcing proxy-throughput with http://old-server.example.com/blah/index.php
go-ahead with proxy request proxy:http://old-server.example.com/blah/index.php [OK]
This is the way Apache has worked since version 2.0: It gets a request, runs that request through all of the RewriteRules, then if the resulting request is a directory, it tries to see if the first DirectoryIndex will work, so it runs that through the RewriteRules and tries to serve the result. If the first DirectoryIndex entry doesn't work (i.e. it gets to the point where Apache would have served a 404), the server tries again with the next entry and so on. Your problem, obviously, is that the proxy rules latch on to the nonexistent first DirectoryIndex value and try to send it through the reverse proxy.
I haven't found a general way to fix this behavior server-wide, but there is a workaround. Use either a <Location> section in your httpd.conf file or a .htaccess file and override DirectoryIndex for that location.
In your case, create the file /var/www/html/blah/.htaccess and put the following into it:
DirectoryIndex index.html

Apache Mod Rewrite for Pretty URLs isn't working

I'm trying to figure out how to do an apache mod_rewrite to remap $_GET.
What I'm trying to accomplish:
Currently, to get to the page one would have to go to
http://www.domain.com/index.php?URL=pages/the-page.php
I would like this to work in 2 ways:
If someone goes to domain.com/the-page, it takes them to the above but keeps it looking like this. Secondly, if someone goes to the http://www.domain.com/index.php?URL=pages/the-page.php, it will still show as domain.com/the-page, keeping the URL short and clean.
Most Recently Tried Code
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} URL=pages/([a-z0-9-_]+)\.php$
RewriteRule ^(.*) /%1
I'm pretty sure I setup everything right in the apache httpd.conf. I'm using XAMPP to test locally, restarted apache on changes, still nothing. Where am I going wrong?
I would prefer to handle this in .htaccess
I am using XAMPP localhost and trying on live server.
Log File:
127.0.0.1 - - [05/Apr/2013:16:50:43 --0400] [localhost/sid#2f3140][rid#3b14068/initial] (3) [perdir C:/xampp/htdocs/cdi/] strip per-dir prefix: C:/xampp/htdocs/cdi/index.php -> index.php
127.0.0.1 - - [05/Apr/2013:16:50:43 --0400] [localhost/sid#2f3140][rid#3b14068/initial] (3) [perdir C:/xampp/htdocs/cdi/] applying pattern '^(.*)' to uri 'index.php'
127.0.0.1 - - [05/Apr/2013:16:50:43 --0400] [localhost/sid#2f3140][rid#3b14068/initial] (1) [perdir C:/xampp/htdocs/cdi/] pass through C:/xampp/htdocs/cdi/index.php
Updated log with Olaf's script (last rule commented out)
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (3) [perdir C:/xampp/htdocs/cdi/] strip per-dir prefix: C:/xampp/htdocs/cdi/index.php -> index.php
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (3) [perdir C:/xampp/htdocs/cdi/] applying pattern '^' to uri 'index.php'
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (3) [perdir C:/xampp/htdocs/cdi/] strip per-dir prefix: C:/xampp/htdocs/cdi/index.php -> index.php
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (3) [perdir C:/xampp/htdocs/cdi/] applying pattern '^index\.php$' to uri 'index.php'
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (2) [perdir C:/xampp/htdocs/cdi/] rewrite 'index.php' -> '/newhome?'
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (3) split uri=/newhome? -> uri=/newhome, args=<none>
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (2) [perdir C:/xampp/htdocs/cdi/] explicitly forcing redirect with http://localhost/newhome <--this one seems to be causing the issue
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (1) [perdir C:/xampp/htdocs/cdi/] escaping http://localhost/newhome for redirect
127.0.0.1 - - [05/Apr/2013:20:02:24 --0400] [localhost/sid#2e3140][rid#3b14090/initial] (1) [perdir C:/xampp/htdocs/cdi/] redirect to http://localhost/newhome [REDIRECT/302]
Thank you everyone that is helping. I've spent 2 days trying to get this to work!!!
Basically, you need two rules. One rule to redirect the client to a clean URL and another to internally rewrite the pretty URL to the real content via index.php.
Assuming the index.php and .htaccess is in a directory cdi
RewriteEngine on
# prevent endless loop
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
# redirect the client
RewriteCond %{QUERY_STRING} URL=pages/(.+?)\.php
RewriteRule ^index\.php$ /cdi/%1? [R,L]
# exclude rewriting all files located in /cdi/files
RewriteCond %{REQUEST_URI} !^/cdi/files/
# rewrite to real content
RewriteRule ^.*$ /cdi/index.php?URL=pages/$0.php [L]
Update:
When the request is /cdi/index.php?URL=pages/abc.php, the second rule extracts the needed URL part and redirects the client to the new URL path. The client then requests the new URL /cdi/abc and the third rule takes this and does an internal rewrite to the real content.
This all works fine as it should, but would rewrite and redirect indefinitely. To break this endless rule, the first rule checks the environment %{ENV:...}, if the request was already redirected REDIRECT_STATUS and then stops the cycle with the RewriteRule
RewriteRule ^ - [L]
which matches everything ^ and does no substitution, but ends the rewrite cycle with the flag [L]
Instead of using the system provided environment STATUS/REDIRECT_STATUS, you can also set a variable yourself with the flag E=SEO:1 for example, and then test for this variable with
RewriteCond %{ENV:REDIRECT_SEO} 1
For the REDIRECT_ prefix, see Available Variables.
You could try this:
RewriteRule ^/([a-z0-9_-]{1,40})/?$ index.php?URL=pages/$1.php
Though ideally you might want to get rid of the "pages/" part of the query string variable, as this fixed constant could be handled by the index.php script.
You approach seems fine but your RewriteCond doesn't match your requirements:
RewriteCond %{REQUEST_URI} ^index.php?URL=pages
means "rewrite the URL if someone requests something that starts with 'index.php"—but that's not what anyone will be requesting. You want your visitors to request pretty URLs.
If your server only needs to serve those requests for /the-page, you can drop the condition entirely. Then any URL will be rewritten. (Note: This might not be what you want!)
Otherwise, the condition should read something like this:
RewriteCond %{REQUEST_URI} ^[a-z0-9-_]{1,40}
If you don't want to mess with regular expressions, you could also try this:
RewriteCond %{REQUEST_FILENAME} !-f
which means "if the user requests a URL for which no file can be found, rewrite the URL according to the upcoming RewriteRule."
If you want the group ([0-9]+) to be alphabetic then just change it to ([a-z]+) and if you've wanted it to be alphanumeric, then change it to ([a-z0-9]+), and ([a-z0-9-_]+) if with a hyphen and an underscore. If you've wanted it to set their limits manually, you can do that with this format ([a-z0-9-_]{1,40}). Do you see, the plus sign is gone, for it limited the [chars] with 1 to anything, and the {1,40} limited the [chars] with 1 to 40, you can either change it.
Do you know what the real problem is? Is my stress.. Imagine even I know that you want to remap /$var into /index.php?URL=pages/$var.php I'm still trying giving you a wrong information that will rewrite /index.php?URL=pages/$var.php into /$var. I just have realize that after my 4 hours sleep. Did you see what's happening when the time of your sleep isn't right? Maybe a rule I would gives to you when my brain's in functioning well, was:
RewriteRule ^([a-z0-9-_]+)/?$ /index.php?URL=pages/$1.php
Why did the viewers letting this to happened.. My previous codes are needed to be voted down.