rewrite rule on root index.php - apache

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.

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]

My Apache rewrite code (provided) in .htaccess works, but also re-writes favicon.ico. How fix?

I have always used this simple code to have all requests use one index.php file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? index.php [L]
However, I have just found that it's also re-writing the favicon.ico file.
My understanding is that the code above should rewrite only index.php and no other file or directory. Am I wrong? How would I fix this?
Just add an exception for it in your rules.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/favicon.ico [NC]
RewriteRule .? index.php [L]

Apache rewrite empty regex (root)

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]

.htaccess file is not working properly with slimphp framework

I am using Slim php framework, having no .htaccess file in root folder I can call the pages
like /index.php/pagename
but if I upload the following .htaccess file in the root folder I always get 403 Forbidden error like You don't have permission to access /hello on this server.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Try changing your rule to this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]
Then you can use a URI /hello which will be rewritten to /index.php/hello.
Please review the Route URL Rewriting
section of the Slim documentation. The .htaccess provided there is different than those posted here.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,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.