Apache rewrite empty regex (root) - apache

I've got Apache 2.4.4 running on Windows 8 laptop (WAMP server) and I found a really odd behavior of .htaccess mod_rewrite rules.
I want to redirect the root of my website to a specific file. My .htaccess looks like this:
RewriteEngine On
RewriteBase /
RewriteRule $^ /static/home.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ router.php?url=$1 [L,QSA]
This works only if there's no index.php file in root directory. When I create index.php file, Apache redirects empty path straight to the index file and doesn't bother with the first RewriteRule.
Is there a way to have both these RewriteRules and index.php file working together? In oher words, my example works but I want to rename router.php to index.php and keep both RewriteRules working.
Thanks!

You can just tweak your regex pattern to work in both situation:
DirectoryIndex something-else.php
RewriteEngine On
RewriteRule ^$ /static/home.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

Related

Apache rewrite /product/ to /products/ and index.php

I've been asked to help on an ExpressionEngine site that's been developed. There is an existing .htaccess file that rewrites anything that isn't a file or directory so that it goes via index.php:
RewriteEngine On
RewriteBase /
# Not a file or directory (also catches index.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
This works fine, and an example URL might be:
www.mysite.com/products/foo (plural 'products')
However, It also needs to deal with old URLs which will be in the form:
www.mysite.com/product/foo (singular 'product')
I've tried the following:
RewriteEngine On
RewriteBase /
# Not a file or directory (also catches index.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/(.+) /index.php/products/$1 [L]
RewriteRule ^(.*)$ /index.php?$1 [L]
Hoping that if it finds 'product' then it rewrites to 'products' via index.php and then quits, but it's not working.
At the same time, I need any incoming URL that starts:
/department/foo
to be rewritten to:
/departments/category/foo
Any help greatefully received.
You can use add a rule above your existing rule to for mapping product -> products:
RewriteEngine On
RewriteBase /
RewriteRule ^product(/.*)?$ products$1 [L,NC]
# Not a file or directory (also catches index.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]

rewrite rule on root index.php

I am trying to rewrite the url on only the root index.php file. I do not want to rewrite the url for any directories.
I am using this in htaccess and it works as far as the rewrite of the index.php just fine.
I looked through stackoverflow, if this post already exists, I didn't see it.
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) index.php?alias=$1 [QSA,L]
Thanks!
To be able to execute this rule in only root directory you can use this rule:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?alias=$1 [QSA,L]
Regex [^/]+ will make sure to not match anything but the root directory.

Cannot properly merge two .htaccess

There are 2 .htaccess files, one in / and the second in /web folders.
In a root folder (/), .htaccess looks like
RewriteEngine on
RewriteRule ^$ web/ [L]
RewriteRule (.*) web/$1 [L]
In /web folder, .htaccess looks like
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
It works perfectly.
All I want is to merge those conditions and put them inside a single .htaccess that would be in root directory (/).
1 - I've tried to add RewriteBase, like
RewriteBase /web
2 - I've tried to replace conditions that aware of web folder
RewriteRule ^(.*)$ web/index.php/$1 [QSA,L]
3 - I've tried to simply append a config from one file to another with slight alterations.
But none of this works - It gives internal 500 server error.
How a proper merging should be done in this case?
This .htaccess (in the root folder) should match to your needs :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/index.php/$1 [QSA,L]
If you're putting all the rules in your document root (/) then try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /web/index.php/$1 [QSA,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]

Htaccess Two different redirects

I have an existing htaccess that works fine:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.*) /default.php
DirectoryIndex index.php /default.php
I wish to modify this so that all urls that start with /test/ go to /test/default.php, while keeping all other URLs with the existing /default.php.
Example: http://www.x.com/hello.php -- > http://www.x.com/default.php Example: http://www.x.com/test/hello.php -- > http://www.x.com/test/default.php
Put the rule for /test/ in before the rule for everything else, but give it an [L] flag to stop the rewrite rule processing there if it matches.
Try this
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (/test/)?(.*) $1/default.php
DirectoryIndex index.php /default.php
This should work:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (test/)?(.*) $1default.php [L]
DirectoryIndex index.php /default.php
Try this.
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule /test/.* /test/default.php
RewriteRule .* /default.php
DirectoryIndex index.php /default.php
You also don't need the ()'s because your not setting the value as a variable.
In your main folder, use the following .htaccess
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule .* default.php [L]
DirectoryIndex index.php default.php
In your ./test/ folder (I assume it is a physical folder), copy and paste the same .htaccess file.
First, you didn't need the / at the beginning of default. Apache will always assume it is looking in the same directory as the .htaccess file is located.
Second, Apache looks backwards when looking for an .htaccess file. So anything in ./test/.htaccess will overwrite what's written in ./.htaccess.