RewriteCond for either .com or .org - regex

Is there a way I can simplify this code so I dont have to check .com and .org separately?
RewriteCond %{HTTP_HOST} ^(www\.)?mywebsite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydirectory(/.*)?$ [NC]
RewriteRule ^(.*)$ /mydirectory/$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mywebsite\.org$ [NC]
RewriteCond %{REQUEST_URI} !^/mydirectory(/.*)?$ [NC]
RewriteRule ^(.*)$ /mydirectory/$1 [QSA,L]

Yes you can take regex help for that. Try this:
RewriteCond %{HTTP_HOST} ^(www\.)?mywebsite\.(com|org)$ [NC]
RewriteCond %{REQUEST_URI} !^/mydirectory(/.*)?$ [NC]
RewriteRule ^(.*)$ /mydirectory/$1 [L]
Take note of (org|com) which makes that patter match both .org and .com

Related

Apache Wildcard Rewriting Subdomain to a Sub Directory

Been trying to Rewrite wildcard subdomain to a subdirectory and not having allot of luck.
ie
abc.domain.com > www.domain.com/sub/abc
I have tried the folowing
// example 1
RewriteCond %{HTTP_HOST} ^(.*).domain.com [NC]
RewriteRule ^(.*)$ ./sub/$1 [L]
// example 2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(.*).domain.com [NC]
RewriteRule ^(.*)$ ./sub/$1 [L]
Hope someone can advise.
You need to use backreference from RewriteCond:
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^((?!(?:sub|index\.php)/).*)$ ./sub/%1/$1 [L,NC]

htaccess redirect conflict/not working

I have a QR code that I need to redirect to a new page. For some reason the redirect is being overridden (ends up at cmdgroup.com/community instead of the appropritate page)
RewriteCond %{HTTP_HOST} ^(.*)?companies/Inova-Solutions-Inc/Network-Clocks-by-Inova-Solutions/53e4f5e048e43c1d5cef656d [NC]
RewriteRule ^(.*)$ http://www.cmdgroup.com/companies/1142841/inova-solutions-inc/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*)?community.cmdgroup.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(.*)?community.reedconstructiondata.com [NC]
RewriteRule ^(.*)$ http://www.cmdgroup.com/community/ [R=301,L]
It is because your first RewriteCond %{HTTP_HOST} is wrong since you only match domain name using %{HTTP_HOST} variable. Change that rule to this:
RewriteRule companies/Inova-Solutions-Inc/Network-Clocks-by-Inova-Solutions/53e4f5e048e43c1d5cef656d$ http://www.cmdgroup.com/companies/1142841/inova-solutions-inc/ [R=301,L]
RewriteCond %{HTTP_HOST} community\.(cmdgroup|reedconstructiondata)\.com$ [NC]
RewriteRule ^ http://www.cmdgroup.com/community/ [R=301,L]

how to make mod_rewrite to redirect from subdomains to querystring?

I want to make mod rewrite as the following example :
from google.com.mydomain.com to mydomain.com/index.php?domain=google.com
I use like
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{QUERY_STRING} !domain=
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mydomain.com$ [NC]
RewriteRule ^(.*)$ /index.php?domain=%1 [L,QSA]
but this redirect google.mydomain.com to mydomain.com/index.php?domain=google
I want rule to redirect google.com not google
thanks
It is due to the incorrect regex. Try this rule:
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{QUERY_STRING} !domain=
RewriteCond %{HTTP_HOST} ^(.+?)\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ /index.php?domain=%1 [L,QSA]
In your rule you're using [^\.]+ which matched until a dot is found therefore it is matching google instead of google.com.

Rewrite subdomain exactly like the main website

I'm currently working on a blog portal, and I'm trying to achieve a specific thing here.
This is my .htaccess code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.[NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.MyDomain.com$ [NC]
RewriteRule ^$ blogdisplay.php?page=%1 [L,QSA]
RewriteRule ^([a-zA-Z0-9-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ index.php?page=$1&page2=$2
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ index.php?page=$1&page2=$2
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ index.php? page=$1&page2=$2&page3=$3
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ index.php?page=$1&page2=$2&page3=$3
When a person is trying to reach SOMETHING.mydomain.com, htaccess is rewriting it to blogdisplay.php?page=BLOGNAME. Just as planned. Now, the problem is that I want it to behave exactly like the main website and rewrite SOMETHING.mydomain.com/page to blogdisplay.php?page=BLOGNAME?page2=page.
Is there any way to make the subdomain use blogdisplay.php instead of index.php, but otherwise work exactly like the subpages of the "main website"?
Replace your code with this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.[NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.MyDomain.com$ [NC]
RewriteRule ^$ blogdisplay.php?page=%1 [L,QSA]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f
## don't do anything
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.MyDomain\.com$ [NC]
RewriteRule ^([a-z0-9-]+)/?$ blogdisplay.php?page=%1&page2=$1 [L,QSA]
RewriteRule ^([a-z0-9-]+)/?$ index.php?page=$1 [L,NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/?$ index.php?page=$1&page2=$2 [L,NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ index.php?page=$1&page2=$2&page3=$3 [L,NC]

403 url rewrite error issue

Hi guys I a major issue with url rewrite. Apologizes if you might have seen this somewhere before.
issue here
If i enter a url for example exampl.x10.mx OR www.example.x10.mx I get a 403 error which shouldnt happen because
RewriteCond %{HTTP_HOST} ^example.x10.mx [NC]
RewriteRule ^(.*)$ http://www.example.x10.mx/$1 [R=301,L]
is MIGHT to take care of that.
RewriteCond %{REQUEST_URI} !^lwh/
RewriteCond $1 !^lwh/
The code above hiden the lwh folder.
FULL .htaccess CODE
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^lwh/
RewriteCond $1 !^lwh/
RewriteCond %{HTTP_HOST} ^example.x10.mx [NC]
RewriteRule (.*) /lwh/main/pages/general/$1 [L]
RewriteRule ^(.*)$ lwh/$1 [L]
RewriteRule ^(.*)$ http://www.example.x10.mx/$1 [R=301,L]
Summary of the problem
If i remember
RewriteCond %{REQUEST_URI} !^lwh/
RewriteRule ^(.*)$ lwh/$1 [L]
the code below works and the same happens if i remember the code below. The thing is I need both of them.
RewriteCond %{HTTP_HOST} ^example.x10.mx [NC]
RewriteRule ^(.*)$ http://www.example.x10.mx/$1 [R=301,L]
An idea why this is happening please
Replace your .htaccess with this code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.x10\.mx$ [NC]
RewriteRule ^(.*)$ http://www.example.x10.mx/$1 [R=302,L]
RewriteCond %{REQUEST_URI} !^/lwh/
RewriteCond %{HTTP_HOST} ^example\.x10\.mx$ [NC]
RewriteRule ^(.*)$ /lwh/main/pages/general/$1 [L]
The problem was with the
R=301 (permanent redirect to new URL)
Before
RewriteRule ^(.*)$ http://www.example.x10.mx/$1 [R=302,L]
now
RewriteRule ^(.*)$ http://www.example.x10.mx/$1 [L]