Htaccess Two different redirects - apache

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.

Related

How to make .htaccess rewrite rule for request filename work with directories with index files?

This is the .htaccess I am using.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -d
RewriteRule (.*)$ https://sub.example.com/$1 [R=301,L]
</IfModule>
When a specific file path like path1/path2/index.html is requested, it works fine and and does't rewrite. But if a path like path1/path2/ is requested, it redirect even if there is a DirectoryIndex file like index.html in that directory. What is an easy way to fix this?
As I suggested in comment above, something like this should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . https://sub.example.com%{REQUEST_URI} [R=301,L,NE]

.htacces rewrite all to index / ignore images and css files

I use the following .htaccess file to send all my requests to the index file.
But now I would like to ignore everything that comes from the /static folder.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
So here's the thing:
- index.php
- static/
----img
--------bg.jpg
----css
--------main.css
Currently it is so that on subpages of the style and the images can no longer be loaded. And I would like to renounce the use of complete paths.
You may try this rule:
RewriteEngine on
RewriteCond %{THE_REQUEST} !/static/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Using THE_REQUEST is preferred for negative conditions as THE_REQUEST variable is not overwritten with other rules.
Well, you need to add an exception, right?
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/static/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [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.

strange redirect with url rewrite

My .htaccess file is:
RewriteEngine on
RewriteRule (.*) index.php?_url=$1 [QSA,L]
It works perfectly in most cases. But if the requested url exists as a real folder, it goes crazy.
For example when i request /random it is rewritten to index.php?_url=random with status code 200, but for /static (which is an existing directory on the web server) it gives a 301 redirect to /static/?_url=static
use this below code for .htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If you want to exclude existing files and directories from being rewritten, do so with a RewriteCond
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?_url=$1 [QSA,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]