Moving symfony2 htaccess file into vhost - mod-rewrite

I wanted to move .htaccess content into vhost for performance, and am trying to solve an issue.
This is in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
I tried this in Vhost:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]
The problem is that second one redirects /app_dev.php/ controller to app.php and it shoud not, like in first example.
Any tips are greatly appreciated.

From RewriteCond Directive Apache Docs:
REQUEST_FILENAME:
The full local filesystem path to the file or script matching the request, if this has already been determined by the server at the time REQUEST_FILENAME is referenced. Otherwise, such as when used in virtual host context, the same value as REQUEST_URI.
So what you are actually testing for is not a file has the path /app_dev.php which is not the absolute path i.e. without the DocumentRoot path prepended. So, you have to do this:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteRule ^ /app.php [L]
^(.*)$ there is no need for this as you are just rewriting without consideration for URI.
Flag QSA also not required as you are not manipulating any query string.

Related

Apache2 - It seems that rewrite condition is not always applied

I would like to redirect to /lost/index.php only when the image file does not exists.
When I try it - it seems that it does not work on browser refresh
If I invoke it with a file which exists on the server
/images/image1.jpg
it shows me a file (GOOD - the file exists )
but if I refresh the browser it redirects me to /lost/index.php (WHICH IS BAD)
Below my rules
RewriteEngine On
RewriteLog /var/log/apache2/rewrite.log
RewriteLogLevel 3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/images/(.*)$ /lost/index.php?image=$1 [L,R]
Any ideas ?
So I solved it.
If you define your rewrite rules inside element they should look like
<Directory /var/www/local.example.com>
...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/(.*)$ /lost/index.html?image=$1 [R]
</Directory>
If you define them "globally" outside the (per-server context) they should look like
RewriteEngine On
RewriteCond %{LA-U:REQUEST_FILENAME} !-l
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
RewriteCond %{LA-U:REQUEST_FILENAME} !-f
RewriteRule ^/images/(.*)$ /lost/index.html?image=$1 [R]
This is due the fact that if used in per-server context (i.e., before the request is mapped to the filesystem) SCRIPT_FILENAME and REQUEST_FILENAME cannot contain the full local filesystem path since the path is unknown at this stage of processing. Both variables will initially contain the value of REQUEST_URI in that case. In order to obtain the full local filesystem path of the request in per-server context, use an URL-based look-ahead %{LA-U:REQUEST_FILENAME} to determine the final value of REQUEST_FILENAME.
What is also important - the pattern has to be different in both cases. In second case it has to starts with '/' while in the first one not. This is due the fact that REQUEST_FILENAME contains the '/' at the beginning for the second case but it does NOT for the first one

.htaccess works in local but not on server (but no 404 error)

I have a .htaccess that is supposed to rewrite my URL. My host has told me that it supports URL rewriting, and I verified that by using phpinfo() and checking.
Anyways, this is my .htaccess:
RewriteEngine On
RewriteRule ^([_a-zA-Z0-9]+)$ index.php?page=$1 [R]
It works like a charm in local, but on my server, it doesn't do anything.
I checked this before on the internet and some people had it, but they all had a 404 error, while I don't have a 404 error. It simply doesn't redirect, it doesn't do anything, so I get all kind of error messages.
RewriteRule ^([_a-zA-Z0-9]+)$ index.php?page=$1 [R]
The regex in your rule doesn't match strings with slashes at any position. I am not sure that's acceptable and you don't give any request examples, but I don't think it is.
You may try this rule-set in one .htaccess file at root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L]
For permanent redirection replace [L] with [R=301,L].
You can make sure that the file (!-f) or directory (!-d) that you're matching doesn't exist before the rewrite. That way you don't end up with a 500 loop with something like /index.php?page=index. Additionally the ^ character is matching the beginning of the string, so if your original test was in a subdirectory it would not rewrite since you weren't allowing slashes.
This should work for any instance, however it will ONLY make the page variable the last string in the URI.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([_a-zA-Z0-9]+)$ /index.php?page=$1 [R,L]

mod_rewrite forward shortend URL

I am looking for a way to create a short URL path for a longer URL on my page
the long url is: domain.com/tagcloud/user.html?t=1234ABCD
i would like to offer a short version of the URL to easy access it:
domain.com/t/1234ABCD
I tried a few examples but I just don't get it how I could forward these rules.
RewriteRule ^(.*)/t/$ /tagcloud/user.html?t=$1 [L]
I am also using MODX so they already use rules.
in addition my htaccess file
RewriteEngine On
RewriteBase /
# Always use www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I must keep the code snippets above in my htaccess file. The first one simply forwards http://domain.com requests to www.domain.com
The friendly URLs part is needed to translate the internal IDs of my CMS with the alias of the URL. This feature must remain because the entire site cannot be influencted by the changes I try to make in htaccess...
I simply would like to add a listener that only if the URL matches www.domain.com/t/abcd1234
Therefore I need something that identifies the www.domain.com/t/ URL
your help is much appreciated
Try this:
RewriteCond %{REQUEST_URI} ^/t/.*
RewriteRule ^t/(.*)$ /tagcloud/user.html?t=$1 [R=301,L]

How to do this via mod_rewrite

http://mydomain.com/bubba
goes to
http://mydomain.com/myscript.php?name=bubba
But does not match anything with an extension on it (.php, .html, etc).
I've been working on this for the last several hours and I cannot see how to do it. Every piece of documentation I find specifically doesn't work.
I'm doing this on a shared host (1and1.com) with .htaccess
Rewrites every non-existant file to myscript.php?name=requested:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ myscript.php?name=$1 [QSA,L]
Maybe you want RewriteCond -f, to check whether the file exists.
To only match things without a period:
^([^.]+)$ myscript.php?name=$1 [QSA,L]

Rewrite URLs for static content

I have problems with static files url rewriting in current .htaccess setup on apache2.
My app structure is:
/siteroot
/siteroot/app
/siteroot/lib
/siteroot/...
/siteroot/public <- all the static files (images, js, etc.) stored here
/siteroot/index.php
/siteroot/.htaccess
So, i need to rewrite url like /css/style.css to /public/css/style.css. I did that in really simple way, but when the file is not found it causing 10 internal redirects, which is bad. I need somehow to return 404 code if file not found, or just pass it to the next rule. And i dont have any access to site configuration file. Only .htaccess.
The reason why i`m asking this question is that the site was running on nginx and i need to rebuild the same configuration on apache.
Here is my .htaccess file.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Test if a redirect is reasonable:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/public/$0 -f
RewriteRule ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]
If the number of prefixes is limited, you could add another group to your regex:
RewriteRule ^(css|js|images|etc)/.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]

Resources