mod rewrite 3 subdomains redirected to themselves (universal redirect) - mod-rewrite

Currently I am using:
RewriteCond %{HTTP_HOST} ^sub1.test.example.net$ [NC]
RewriteRule ^/?$ https://sub1.test.example.net/folder1/folder2/app/nodes/dir1 [R,L]
RewriteCond %{HTTP_HOST} ^sub2.test.example.net$ [NC]
RewriteRule ^/?$ https://sub2.test.example.net/folder1/folder2/app/nodes/dir1 [R,L]
RewriteCond %{HTTP_HOST} ^sub3.test.example.net$ [NC]
RewriteRule ^/?$ https://sub3.test.example.net/folder1/folder2/app/nodes/dir1 [R,L]
I am trying to condense a universal rule for the 3 different env's to possible 2 lines, for ex:
Rewrite condition: sub1 OR sub2 Or sub3
Rewrite Rule: (sub1 OR sub2 Or sub3).example.net/folder1/folder2/app/nodes/dir1
I apologize for being a newbie, I have been reading the apache mod rewrite man pages for a couple of days but havent been able to crack it. Even a point in the right direction would be a huge help.

You could add an [OR] flag after each condition if you only want to process the rule on specific subdomains but if you just want to match any subdomain you can use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).test.example.net$ [NC]
RewriteRule ^/?$ https://%1.test.example.net/folder1/folder2/app/nodes/dir1 [R,L]
This would for example redirect http://sub777.test.example.net/?test to https://sub777.test.example.net/folder1/folder2/app/nodes/dir1?test

Related

Regard a couple of OR-RewriteCond directives if another RewriteCond evals

I have the following configuration,
RewriteCond %{HTTPS} !^on$ [nocase,ornext]
RewriteCond %{HTTP_HOST} !^www\. [nocase,ornext]
RewriteCond %{HTTP_HOST} !\.com$ [nocase]
RewriteRule ^(.*)$ https://www.acme.com/$1 [redirect=301,last]
the purpose is to redirect to a canonical URL if the request is either not HTTPS, nor begins with www. or ends with .com.
For being seamlessly compatible with developer engines, I want to exclude all these directives if %{HTTP_HOST} includes, for example, dev.internal or so. In this case the RewriteRule should be skipped immediately. Since the three ORs are evaluated with the higher precedence than an (implicit) AND, I wonder how and where to place my dev.internal exception...
Thanks for any advice!
//edit: hmm... if OR has the higher precendence, shouldn't
RewriteCond %{HTTP_HOST} !internal\. [nocase]
RewriteCond %{HTTP:X-Forwarded-Proto} !^https$ [nocase,ornext]
RewriteCond %{HTTP_HOST} !^www\. [nocase,ornext]
RewriteCond %{HTTP_HOST} !\.com$ [nocase]
RewriteRule ^(.*)$ https://www.acme.com/$1 [redirect=301,last]
work then?
If my understanding of ornext is correct then yes, your way should work (can someone else confirm it?).
Here's another way, if you don't want to rely on that:
RewriteCond %{HTTP_HOST} internal\. [nocase] # If it's an internal host...
RewriteRule .* - [skip=1] # ... skip the next rule (and leave the URL unchanged)
RewriteCond %{HTTP:X-Forwarded-Proto} !^https$ [nocase,ornext]
RewriteCond %{HTTP_HOST} !^www\. [nocase,ornext]
RewriteCond %{HTTP_HOST} !\.com$ [nocase]
RewriteRule ^(.*)$ https://www.acme.com/$1 [redirect=301,last]

apache mod rewrite with the_request

I want to do the following with apache (mod rewrite).
if the user requests http://hostname.tld/index.php/folder/subfolder i want it to redirect (with a R=301) to http://hostname.tld/folder/subfolder.
if the user requests http://hostname.tld/folder/subfolder the request should internally be rewritten to index.php/folder/subfolder.
To prevent an endless redirect the first rule should check for %{THE_REQUEST}. The problem here is that I am unable to append "folder/subfolder" with a regex. How should I do this?
For the second rule I have this (and seems to work).
RewriteCond %{HTTP_HOST} hostname.tld [NC]
RewriteRule ^(.*)$ index.php [QSA,L]
The first one is still a problem.
I think the first one should be something like
RewriteCond %{THE_REQUEST} (.*)index.php(.*) [NC]
RewriteRule /index.php/$ http://hostname.tld/$1 [R=301,QSA,L]
But that is not really it.
The first should be.
RewriteCond %{HTTP_HOST} ^hostname\.tld$ [NC]
RewriteCond %{THE_REQUEST} index\.php [NC]
RewriteRule ^index.php/(.*)$ http://hostname.tld/$1 [R=301,L]
I also see that your second rule redirects http://hostname.tld/folder/subfolder to http://hostname.tld/index.php (not http://hostname.tld/index.php/folder/subfolder). But as long as that works it's fine, as this it also prevents the redirect loop.
But just in case, here is the solution to add the folder/subfolder part:
RewriteCond %{HTTP_HOST} ^hostname\.tld$ [NC]
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

Apache mod_rewrite friendly URLs with corresponding 301 redirects

The Problem:
Been spinning my wheels and reading up on this one for awhile and looking for some help now. I'm looking to take a group of non-friendly URLs (there are actually more "groups" but this should me for an example):
domainname.com/?section=zebras
domainname.com/?section=monkeys&id=555
and turn them into friendly URLs, as well as do a 301 on the old versions, so that any old bookmarks (and search engines) will still resolve them. The new format I'm looking for would be:
domainname.com/zebras/
domainname.com/monkeys/555
I'm fully intending to write separate RewriteCond/RewriteRule combinations for each of those scenarios, so I don't necessarily need a super-rule that catches all my scenarios. Oh and this is all in .htaccess.
My Progress:
I was originally getting into a redirect loop because I was just doing two RewriteRules back to back - one for the friendly URL and one for the 301 redirect. Came across my favorite way (so far) around the redirect loop which works (for my scenario #1 at least):
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^section=zebras$ [NC]
RewriteRule ^.*$ http://www.domainname.com/zebras/? [R=301,NC,L]
RewriteRule ^zebras/$ /index\.php?section=zebras [NC,L]
However, I'd like to have something that works for more than just "zebras" (for instance, I'd like it to work for "lions" as well), so I'm trying to make that more generic. What I am trying now looks like this:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^section=([a-z]+)$ [NC]
RewriteRule ^section=([a-z]+)$ http://www.domainname.com/$1/? [R=301,NC,L]
RewriteRule ^([a-z]+)/$ /index\.php?section=$1 [NC,L]
However, this doesn't work. I think I have something "not quite right", I just can't tell what it is - there's something I'm missing or formatting incorrectly somewhere. Sorry in advance for the lengthy description, just wanted to be clear.
Do this:
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.\w+|/)$
RewriteRule (.*) /$1/ [R,L]
RewriteCond %{QUERY_STRING} ^section=([a-z]+)$ [NC]
RewriteRule ^ /%1/? [R=301,NC,L]
RewriteRule ^([a-z]+)/$ /index\.php?section=$1 [NC,L]
RewriteCond %{QUERY_STRING} ^section=([a-z]+)&id=(\d+)$ [NC]
RewriteRule ^ /%1/%2/? [R=302,NC,L]
RewriteRule ^([a-z]+)/(\d+)/$ /index\.php?section=$1&id=$2 [NC,L]
Description
Prevents looping:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
Prevents trailing slash problem:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.\w+|/)$
RewriteRule (.*) /$1/ [R,L]
Handles rewrites with only section=([a-z]+) in them:
RewriteCond %{QUERY_STRING} ^section=([a-z]+)$ [NC]
RewriteRule ^ /%1/? [R=302,NC,L]
RewriteRule ^([a-z]+)/$ /index\.php?section=$1 [NC,L]
Handles rewrites with only section=([a-z]+)&id=(\d+) in them:
RewriteCond %{QUERY_STRING} ^section=([a-z]+)&id=(\d+)$ [NC]
RewriteRule ^ /%1/%2/? [R=302,NC,L]
RewriteRule ^([a-z]+)/(\d+)/$ /index\.php?section=$1&id=$2 [NC,L]
mistake in your rules:
section=([a-z]+) is not available in the URI part. So, RewriteRule ^section=([a-z]+)$ never matched.

Nested subdomain URL rewrite

I have a sight that is of the following form:
nested_subdomain1.nested_subdomain2.domain.com
It might be something like test.users.domain.com and I would like to be able to rewrite this URL to something like test.users.domain2.com.
So far, my luck has not proven well and I have not been able to successfully implement a working solution from examples found online. I have tried some things like the following:
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule (.*) http://domain2.com/$1 [R=301,L]
Or this one...
RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R]
I am not sure what I am doing wrong and feel like I am missing something really obvious.
Try this
#match anything1.anything2.domain.com
RewriteCond %{HTTP_HOST} ^([^.]+\.[^.]+)\.domain\.com$ [NC]
#redirect to anything1.anything2.domain2.com
RewriteRule ^ http://%1.domain2.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} domain\.com$
RewriteRule (*.).mydomain.com mydomain.com/$1
This will trasnfer xx.yy.mydomain.com to mydomain.com/xx.yy
To replace with slashes, try
RewriteCond %{HTTP_HOST} domain\.com$
RewriteRule (*.\.).mydomain.com mydomain.com/$1/$2/$3
To transfer to another domain ,try
RewriteCond %{HTTP_HOST} domain\.com$
RewriteRule (*.).mydomain.com $1.mydomain.com [R=301,L]
This will transfer the subdomains upto a level of three. Frankly, you will have to analyze the host in your index.php to determine which subdomain is caled, so might as well use the first one

How mod_rewrite can add subdomain?

RewriteEngine On
RewriteRule ^/ai?$ /Market/publish.jsp [QSA,L,PT]
RewriteRule ^/ar?$ /Market/MailDispatch [QSA,L,PT]
RewriteCond %{HTTP_HOST} !^web\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://web.example.com/$1 [L,R]
#How skip www\. to web\. for this 1 ?
#RewriteRule ^/vi/?([0-9]+)\.htm$ /Market/vi.do?id=$1 [PT,L]
RewriteRule ^/li /Market/list.do [QSA,PT,L]
RewriteRule ^/vi/locations.jsp /Market/locations.jsp [PT,L]
ErrorDocument 404 /notfound.html
Nearly undoable(?) I try http://example.com/vi/{N}.htm should redirect to http://web.example.com/vi/{N}.htm where N is dynamic ID.
Seen mod_rewrite with subdomain and url pattern
There is no clear way to make eg http://example.com/vi/1096.htm pass up to next version http://web.example.com/vi/1096.htm where number is dynamic. I tried
A rule with the following scheme should do it:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/vi/\d+\.htm$ http://web.example.com%{REQUEST_URI} [L,R=301]
It’s important to put this rule in front of those rules that do an internal redirect. Otherwise an already internally rewritten URL could be rewritten externally.
If you want to use this rule in a .htaccess file, remove the leading slash from the pattern in RewriteRule.

Resources