.htaccess skip rewrite if url equals/contains some words - .htaccess

I have .htaccess like this:
<IfModule mod_php4.c>
php_value session.use_trans_sid 0
</IfModule>
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) $1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L]
but I want to skip these rules if url contains e.g. admin, administration words just after domain name (eg. domain.com/admin/ or domain.com/administration/)

Add the following conditions before your rules:
RewriteCond %{REQUEST_URI} !^/admin/
RewriteCond %{REQUEST_URI} !^/administration/

Related

.htaccess redirect all pages of old site to same pages on a subdirectory of a different site

I need to redirect all pages of an old site to a subdirectory of our main site. E.g.:
oldsite.com/page1
to
mainsite.com/oldsite/page1
I have tried adding the following to the top of .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsite.com$
RewriteCond (.*)$ http://www.mainsite.com/oldsite/$1 [R=301,L]
</IfModule>
When I open a fresh anonymous window, the redirect does not work, even on the homepage. So it is probably another rule getting in the way. The following is at the bottom of the .htaccess file:
# BEGIN W3TC Page Cache core
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} =on
RewriteRule .* - [E=W3TC_SSL:_ssl]
RewriteCond %{SERVER_PORT} =443
RewriteRule .* - [E=W3TC_SSL:_ssl]
RewriteCond %{HTTP:X-Forwarded-Proto} =https [NC]
RewriteRule .* - [E=W3TC_SSL:_ssl]
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteRule .* - [E=W3TC_ENC:_gzip]
RewriteCond %{HTTP_COOKIE} w3tc_preview [NC]
RewriteRule .* - [E=W3TC_PREVIEW:_preview]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} =""
RewriteCond %{HTTP_COOKIE} !(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle) [NC]
RewriteCond %{REQUEST_URI} \/$
RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_SSL}%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" -f
RewriteRule .* "/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_SSL}%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" [L]
</IfModule>
# END W3TC Page Cache core
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{REQUEST_FILENAME} (.*)\.(jpe?g|png)$
RewriteCond %{REQUEST_FILENAME}\.webp -f
RewriteCond %{QUERY_STRING} !type=original
RewriteRule (.+)\.(jpe?g|png)$ %{REQUEST_FILENAME}.webp [T=image/webp,E=accept:1,L]
</IfModule>
<IfModule mod_headers.c>
Header append Vary Accept env=REDIRECT_accept
</IfModule>
Any ideas on what I should change to achieve my desired result?:
oldsite.com/page1
to
mainsite.com/oldsite/page1
Solved!
I removed all the rewrite rules that were already in there, apart from the wordpress one.
I then used this rule instead of the one I originally posted:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsite.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.com$
RewriteRule (.*)$ http://www.mainsite.com/oldsite/$1 [R=301,L]
</IfModule>

Htaccess RewriteRule change subdomain url

I'm in this situation
http://example.com/sub-domain/
and I wish to have this
http://example.com/sub/domain/
without loosing in SEO, what's the right RewriteRule?
At the moment this is my .htaccess file in the subdomain DOCUMENT_ROOT
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /sub-domain/
# exclude any paths that are not codeigniter-app related
RewriteCond %{REQUEST_URI} !^/server-status
RewriteCond %{REQUEST_URI} !^/server-info
RewriteCond %{REQUEST_URI} !^/docs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
# the following is for rewritting under FastCGI
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
EDIT
In my root domain I have installed Wordpress and this is the .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [QSA,L]
</IfModule>
Leave /sub-domain/.htaccess as is. And have this rule in root .htaccess (a level above /sub-domain/):
RewriteEngine On
RewriteCond %{THE_REQUEST} /sub-domain(\S*)\s [NC]
RewriteRule ^ /sub/domain%1? [R=301,L,NE]
RewriteRule ^sub/domain(/.*)?$ sub-domain$1 [L,NC]

A bad use of RedirectPermanent

I try actually to change my url => www.mysite.com to www.mysite.com/fr/ and i have to use my .htaccess for that.
So i changed my .htaccess from my root like that:
SetEnv SHORT_OPEN_TAGS 0
SetEnv REGISTER_GLOBALS 0
SetEnv MAGIC_QUOTES 0
SetEnv SESSION_AUTOSTART 0
SetEnv ZEND_OPTIMIZER 1
SetEnv PHP_VER 5_3
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [QSA,L]
RedirectPermanent / http://www.chat.com/fr/
</IfModule>
and i have my /web/.htaccess like that:
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
and when i try to upload my site, i have this url : www.mysite.com/fr/fr/fr/fr/... and an error. Someone can help me ? :(
Your rules will indeed cause a redirect loop. Try this in root .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/?$ /fr/ [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [L]
</IfModule>
Make sure to test this after clearing your browser cache.

Block 'kw=' Query String

I keep getting nonsense hits to my site like:
myurl.com/?kw=antivirus*protection
myurl.com/?kw=a+casino
myurl.com/?kw=.confused.com
myurl.com/?kw=whatevernonsense
Also, if it matters, it is a self-hosted Wordpress site. I've tried many permutations to the following, but it isn't working:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} kw
RewriteRule ^(.*)$ - [F,L]
</IfModule>
Edit: I've tested the above code in a separate directory, and it works fine. Below is the Wordpress chunk I've narrowed it down to:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} kw
RewriteRule ^(.*)$ - [F,L]
</IfModule>
# Use PHP5.3 Single php.ini as default
AddHandler application/x-httpd-php54s .php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I was able to fix this by adding the following to the WP chunk of the .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
# These 2 lines block any request coming in with `/?kw=`
RewriteCond %{THE_REQUEST} \s/+[^?]*\?kw=
RewriteRule ^ - [F,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

mod rewrite .htaccess URL redirect

I want to simply point the www.mysite.com/corporate to www.mysite.com/services/corporate
here is my .htaccess:
#DirectoryIndex index.php index.html
#Options +FollowSymLinks
#Options -Indexes
#ErrorDocument 403 default
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^corporate$ services/corporate
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*) index.php
RewriteCond %{HTTP:Authorization} !^$
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
</IfModule>
#php_value register_globals 0
Use mod_rewrite. Place something like this in .htaccess in the root directory:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/corporate/(.*)$ $1/services/corporate/$2 [R,L]
See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html for more info.
Difficult to understand some parts of your .htaccess code but you can add one more line before </IfModule> there:
RewriteRule ^corporate/?$ services/corporate [NC,L,R=301]

Resources