htaccess exclude a sub/subdirectory from a 302 redirect - apache

I'm trying to exclude a directory that's 1 level deeper than one that already has a redirect.
So Redirect /department/team/
but exclude /department/team/employee/
I've already tried
RewriteCond %{REQUEST_URI} !^/department/team/employee/
and
RewriteRule ^/department/team/employee/ - [L]
Here's current htaccess.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)/$ $1 [R=301]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
##RewriteCond %{REQUEST_URI} !^/department/team/employee/
RewriteRule ^/department/team/employee/ - [L]
Redirect 302 /department/team/ https://example.com

Related

htaccess Redirect all except the specified URL

Cannot use (!) reverse operation, a redirect URL (http://ssvwv.com) will appear:
/1/3/ktrb
Will not display (index.html)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^\/1\/(?:1|2|3)\/.*$
RewriteRule ^(.*)$ http://ssvwv.com/ [L,R=302,NE]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . /index.html [L]
This should work for you:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(?:index\.html|1/[1-3]/.*)$ [NC]
RewriteRule ^ http://ssvwv.com/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.html [L]
Issue is that last rule is rewriting to /index.html and when mod_rewrite loops again URI becomes /index.html and RewriteCond %{REQUEST_URI} !^\/1\/(?:1|2|3)\/.*$ returns true thus redirecting to http://ssvwv.com/. That's why you need to skip /index.html from first rule as well.

htaccess: redirect of index.php

Sorry for not-very-clear title.
I'm using FatFreeFramework with his .htaccess:
RewriteCond %{REQUEST_URI} \.ini$
RewriteRule \.ini$ - [R=404]
#RewriteCond %{REQUEST_URI} \.html?$
#RewriteRule \.html?$ - [R=404]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA,E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Now, restyling an old site, I have in Google some url codified like
www.example.com/index.php?ID=12
I would strip away that and redirect them to home page.
I don't know how write the rule, I did try to edit in this mode but without luck:
RewriteCond %{REQUEST_FILENAME} !-d
Redirect 301 /index.php?ID=$1 index.php
RewriteRule .* index.php [L,QSA,E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Right above this rules:
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA,E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Add this:
RewriteCond %{THE_REQUEST} \ /+index\.php\?ID=[0-9]
RewriteRule ^ / [L,R=301]
You can't match against the query string using mod_alias's Redirect directive.

.htaccess redirect behaviour

I have a .htaccess file which redirects the user to https:// and adds /de/index if nothing else is specified.
RewriteEngine on
RewriteRule ^(hotelaccess)($|/) - [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/de/index [L]
RewriteCond %{HTTP_HOST} !^www\.mypage\.de
RewriteRule (.*) http://www.mypage.de/de/index [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&page=$2 [L,QSA]
but now if I enter https://www.mypage.de it get's redirected to https://www.mypage.de/index ? Why is that and what do I have to add to fix this?
Clarification: The default URL should always be https://www.mypage.de/de/index if entering with and without www and both http:// and https:// (also with or without www)
You need to put your redirects first, then your rewrites. You can also combine the two redirects into a single rule:
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTP_HOST} ^mypage\.de$ [NC]
RewriteRule ^(.*)$ https://www.mypage.de/$1 [R,L]
# for no URI
RewriteRule ^$ /de/index [L]
# for hotelaccess
RewriteRule ^hotelaccess - [L]
# for everything else
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&page=$2 [L,QSA]

TYPO3 breaks urls without WWW

The problem is that when I want to access my website like www.example.com/my/subpage all works great but when I try to access my website like this (without WWW) example.com/my/subpage TYPO3 breaks it down and redirect it to www.example.com/index.php/subpage.
The redirect from non WWW to WWW is correct but why does it destroy my url? Here is the .htaccess redirect:
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
For the "nice" urls I use the realURL extension. Any ideas?
Edit (more htaccess):
RewriteRule ^(typo3|t3lib|tslib|fileadmin|typo3conf|typo3temp|uploads|showpic\.php|favicon\.ico)/ - [L]
RewriteRule ^typo3$ - [L]
RewriteRule ^typo3/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php
Keep your 301 rules before other internal rewrite rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^(typo3|t3lib|tslib|fileadmin|typo3conf|typo3temp|uploads|showpic\.php|favicon\.ico)/ - [L]
RewriteRule ^typo3(/.*)?$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php [L]
Also test this in a new browser to avoid browser caching issues.

.htaccess redirect trouble

I'm having trouble with (what should be) a simple .htaccess redirect. I need to changed any url that has "for_homes" into "for_home".
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
However when I go to any pages with for_homes I get a 404.
Rewrite Rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
</IfModule>
The problem is a preceeding rule is rewriting to index.php. In this block here:
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
### This rule rewrites "for_homes" to "index.php?/for_homes"
RewriteRule ^(.*)$ index.php?/$1 [L]
### These rules never get applied
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
So perhaps after the RewriteCond %{REQUEST_FILENAME} !-d, add conditions to exclude the later 2 rewrites, So that it looks like this:
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/for_home(s)?
RewriteCond %{REQUEST_URI} !^/for_business(es)?
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
If it's your intention to have index.php handle for_home and for_businesses, so that when someone enters http://domain.com/for_homes/stuff in their browser's address bar, the browser gets redirected to http://domain.com/for_home/stuff, then it gets internally rewritten to /index.php?/for_home/stuff so that index.php can handle the request, you simply need to move the 301 redirects before the index.php rewrite:
### Redirect first
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]
RewriteRule ^for_business(.*)$ for_businesses$1 [L,R=301]
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Perhaps you are just missing RewriteEngine On
RewriteEngine On
RewriteRule ^for_homes(.*)$ for_home$1 [L,R=301]