force url interpreation with urlrewriting - apache

On my wiki I would like redirect all queries (for example http://mywikiofme.com/images/... ) to a php script script.php
I put this in my virtual host :
RewriteEngine on
RewriteRule ^images/(.*)$ /script.php?url=$1
But it doesn't work if the folder already exist on the server...
If I test :
RewriteEngine on
RewriteRule ^foldernotexist/(.*)$ /script.php?url=$1
The redirection to script.php works fine.
I think I need to use a flag but I don't know which one.
Ideas ?
Thanks

Theoretically - should work =)
<Directory "/htdocs/images">
RewriteEngine on
RewriteBase /images/
RewriteRule ^(.*)$ /script.php?url=$1 [L,QSA]
</Directory>
Where "/htdocs/images" is your path to images folder

Related

.htaccess rule for redirecting to parent

I am trying to redirect one of my urls to the parent folder using .htaccess file. I have tried the following rule
RewriteRule ^test/(.+)$ /test/ [L,R=301]
found from htaccess wildcard redirect to parent folder but it is not working (logs show too many redirects).
I also tried the other rules below but none of them worked
RewriteBase /
RewriteCond %{REQUEST_URI} !=/test/
RewriteRule ^(.*) /test/ [END,NC]
or
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^test/(.+)$ /test/ [L,R=301]
The OS is ubuntu server. Any help or pointers is appreciated. Please let me know if I can furnish any other details to debug. Thanks
Following should work considering the parent directory is test
RewriteEngine On
RewriteRule ^(test/).* /$1 [R=301,L]
Add this to disable MultiViews:
Options -MultiViews
The Apache docs on mod_negotiation, describes what the Multiviews Option does, when enabled:
If the
server receives a request for /some/dir/foo and /some/dir/foo does not
exist, then the server reads the directory looking for all files named
foo.*, and effectively fakes up a type map which names all those
files, assigning them the same media types and content-encodings it
would have if the client had asked for one of them by name. It then
chooses the best match to the client's requirements, and returns that
document.
Use:
Options -MultiViews
RewriteEngine on
RewriteRule ^test/(.+)$ /test/ [NC,L,R=301]
In your specific folder
RewriteEngine On
RewriteRule ^rootFName(.*) /rFile.php [QSA,R=301,L]

Silex in subdirectory on localhost : "Sorry, the page you are looking for could not be found"

I know there are SEVERAL similar questions already asked on this topic. I've looked at all of them and none of them have solved my issues. Here's what i have done so far:
Already considered using a Virtual Host (i dont want to, for several reasons)
Made sure mod_rewrite is on (YES, it is!)
written an .htaccess file that contains the default
Silex Webserver Configuration. posted [here](http://silex.sensiolabs.org/doc/web_servers.html) and is contained inside silex/ directory. code below
Successfully reached the silex/web/index.php page via localhost/~username/silex (so mod_rewrite is definitely working). code below as well
I echo out a "here!" statement and on silex/web/index.php
I tried one solution that consisted of placing a second .htaccess file in the silex/web/ dir
.htaccess file:
Options -MultiViews
RewriteEngine On
## this was uncommented because silex is in a subdir
RewriteBase /~username/silex/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
/silex/web/index.php file:
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
echo "HELLO";
$app->get('/', function() {
return 'Hello!';
});
$app->run();
When I get to the silex/web/index.php here's what I get:
I've been working on this for about 2 days now and am starting to lose it.
any help will be greatly appreciated.
thanks.
AH HA! i found my answer here -> MAMP Silex htaccess.
I'm hoping this solution will help other people from going though my pain! :)
In your .htaccess is missing the rewrite condition for the directory, try this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /~username/silex/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
</IfModule>
or use
FallbackResource /~username/silex/web/index.php
if your Server is running Apache >= 2.2.16.
Expanding on the solution, you need to create an htaccess for the subdirectory containing Silex (and probably Symfony, etc) and tell Apache how to interpret routes. To paraphrase crzpata's Gist:
<IfModule mod_rewrite.c>
RewriteEngine on
#UNCOMMENT next line if silex app root is in a mamp subdirectory
#RewriteBase /your_mamp_htdocs_silex_app_root
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ web/index.php [L]
</IfModule>
Then define your silex routes using that defined root:
$app->get('/directory_in_silex_root'...

apache htaccess rewrite with alias

We are changing our domain name and this is meant to work for stand alone applications. In Apache virtual host file the DocumentRoot is /var/www/website/html, not /var/www/example/html as in this block:
Alias /apps/dept /var/www/example/html/apps/dept
<Directory "/var/www/example/html/apps/dept/">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I put an .htaccess file in /var/www/example/html/apps/dept directory as follows:
RewriteEngine On
RewriteBase /apps/dept/
RewriteCond %{HTTP_HOST} ^example.orgname.state.tx.us/apps/dept [NC]
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [L,R=301]
This seems to follow what is recommended here, http://httpd.apache.org/docs/current/mod/mod_rewrite.html and Apache : How to Use Rewrite Engine Inside Alias. I see no results. The new domain has a virutal host config in the VH file, also. This same basic rewrite works for our Drupal website which does not use an alias. What changes might be necessary to have the domain name rewritten with an appended application pathname? Is the RewriteBase incorrect?
Thx.
So you only want to redirect /apps/dept/, correct? This should work. Place it as an .htaccess or in the Apache config for example.orgname.state.tx.us and all should work as expected.
RewriteEngine on
RewriteRule ^/apps/dept/(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]
So now, any requests going to this URL
http://example.orgname.state.tx.us/apps/dept/
Will now go to this URL:
http://example.orgname.texas.gov/apps/dept/
And any request parameters to the right of the URL will be passed along as well.
EDIT Just reread what you wrote here:
I put an .htaccess file in /var/www/example/html/apps/dept directory
as follows.
The .htaccess I described above should be placed in /var/www/example/html/ and not in the /apps/dept subdirectory.
But if you want the same behavior from an .htaccess placed in /apps/dept then use this:
RewriteEngine on
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]
This way any request made from /apps/dept will to to example.orgname.texas.gov/apps/dept/ including subdirectories of /apps/dept such as /apps/dept/test_app1, /apps/dept/test_app2 or /apps/dept/test_app3.
Or perhaps try this:
RewriteEngine on
RewriteRule (.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]
Note I removed the ^ which would force the RewriteRule to match the beginning of the URL.
You cannot match Request URI in %{HTTP_HOST} variable, it matches only domain name. Chang your rule to:
RewriteEngine On
RewriteBase /apps/dept/
RewriteCond %{HTTP_HOST} ^example\.orgname\.state\.tx\.us$ [NC]
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [L,R=301]

.htaccess RewriteRule adds drive path to URL

I am using Zend Server CE (v.5.1.0) installed on C: on a Win7 machine. I have added one project to httpd.conf by adding:
Alias /project "D:\Homepages\project"
<Directory "D:\Homepages\project">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
My .htaccess file in the project directory contains the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/\w*\.(css|js) [NC]
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Now to the problem; if I go to
http://localhost/project/index.php
everything seems to be working fine. I reach the index.php file and get my contents.
However, if I go to any other page that would trigger the RewriteRule, it seems to be adding the directory path. FireFox outputs the following Not Found message:
The requested URL /Homepages/project/index.php was not found on this server.
I tried to find a similar question/answer here, but failed. Any idea?
Ps. Me accepting of an answer might be delayed as I will be out for a while on an errand.
You need to set the RewriteBase directive; otherwise, mod_rewrite automatically, and by default, prepends the file path to the resulting rewrite rule.
From: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
When a substitution occurs for a new URL, this module has to re-inject the URL into the server processing. To be able to do this it needs to know what the corresponding URL-prefix or URL-base is. By default this prefix is the corresponding filepath itself. However, for most websites, URLs are NOT directly related to physical filename paths, so this assumption will often be wrong! Therefore, you can use the RewriteBase directive to specify the correct URL-prefix.
If your webserver's URLs are not directly related to physical file paths, you will need to use RewriteBase in every .htaccess file where you want to use RewriteRule directives.
Have your last line like this:
RewriteRule ^.*$ /index.php [NC,L]
However I think this is infinite loop so I would suggest this rule instead:
RewriteCond %{THE_REQUEST} !\s/index.php [NC]
RewriteCond %{REQUEST_URI} !^/index.php [NC]
RewriteRule . /index.php [L]
which prevents going to index.php if it is already /index.php.

Use symfony 1.4 without changing apache configuration

Is it possible to set the /web directory as webroot without changing apache configuration file?
I tried using the following .htaccess code, but if i go to localhost/module/, it displays 404 error. But if i go to localhost/web/module/ then everything works.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule sf/(.*) lib/vendor/symfony/data/web/sf/$1 [L]
RewriteRule ^$ web/ [L]
RewriteRule (.*) web/$1 [L]
</IfModule>
i do like this on the root :
RewriteEngine On
RewriteBase /
RewriteRule (.*) ./web/$1 [L]
And edit web/.htaccess uncommented the 'RewriteBase /' line.
this make all the mysite.com/aaaa/bbbb works like mysite.com/web/aaaa/bbbb
Short answer: no.
Bit longer: you will have to edit the apache config at least to give it permission to access the web/ directory, so even if you symlink your web folder to /var/www, it will not work.
This is quiet similar to my question Symfony on virtual host (document root problem).
This is my .htaccess in the project root directory:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/images/ [OR]
RewriteCond %{REQUEST_URI} ^/js/ [OR]
RewriteCond %{REQUEST_URI} ^/css/
RewriteRule ^(.*)$ /web/$1 [L]
RewriteCond %{REQUEST_URI} !^/web/
RewriteRule ^(.*)$ /web/index.php [QSA,L]
This solved my problem but symfony tries to generate every url (eg. using url_for) from the document root so instead of url like domain.com/my-article it generates domain.com/web/my-article.
I had to slightly modify my PatternRouting class to trim the /web prefix from each url. I think this is not the best solution but it works.
Also if I want to access backend application I have to call always /web/backend.php/ because I don't want to have so many rewrite rules in the .htaccess.
If you want to see my extended PatternRouting class source code I'll paste it here.
Yes, it is possible. Copy everything from web/ up a level to your document root. Edit index.php to reflect the fact that everything it includes is now one level closer to its current directory than it used to be (one less ../). You won't have to edit a single other Symfony file.