homepage Welcome to WebmasterWorld Guest from 54.234.0.85
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
dynamic rewrite
cbo0485



 
Msg#: 4618855 posted 5:49 pm on Oct 24, 2013 (gmt 0)

I've been trying to come up with a rewrite that would be dynamic and accomplish the following.

Let's say our domain is https://www.example.com. But we have multiple vanity URL's, something like, https://www.promoabc.com, which points to our apache instance. What we'd like to do is, rewrite promoabc.com into https://www.example.com/promoabc

This way as we buy promocde, promoxyz, etc... we dont need a new rewrite every time. We want to capture what is between www. and .com, and then rewrite it to point back to our main domain but then it'd look for that directory on the end.

promoabc.com becomes
example.com/promoabc

aweomepromo.com becomes
example.com/awesomepromo

An example of how we are doing it today:

RewriteCond %{SERVER_NAME}= ^(.*)(promoabc\.)
RewriteRule ^/(.*) https://example.com/promoabc/ [R=301]



I was thinking something like this, but haven't gotten a chance to test it yet:


RewriteCond %{SERVER_NAME}!= ^(.*example.com)
RewriteCond %{SERVER_NAME}= ^(.*)(.*\.com)
RewriteCond %{DOCUMENT_ROOT}/$i -f
RewriteRule https://example.com/$1

 

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4618855 posted 8:05 pm on Oct 24, 2013 (gmt 0)

You say you want a rewrite but your example code, that you say you already use, is for a redirect.

Which do you want?

cbo0485



 
Msg#: 4618855 posted 8:51 pm on Oct 24, 2013 (gmt 0)

Maybe I used the wrong verbiage, but what I need is to take when a user hits our site by promoabc.com, they get redirected/re-written to example.com/promoabc, in a dynamic fashion.

I believe I have accomplished it. Some quick smoke tests have worked, just need to put it through some more rigorous testing before it moves into higher environments:



RewriteCond %{SERVER_NAME} !^(.*example\.com)
RewriteCond %{SERVER_NAME}= ^(.*)(.*\.com)
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^/(.*) https://example.com/%1


First one checks to make sure I'm not hitting the website using the standard url.
Second one captures everything before the .com and after the www. and stores it in %1
Third one verifies that the directory in our htdocs exists
Last one finally rewrites it to point to the correct location.

And again, the only reason I wanted to do it in this way, is b/c we have many different promotional url's, that all have to hit the same apache web-server, just a different directory within htdocs. This should prevent us from having to add these more static statements every time they come up with a new promotional url

aakk9999

WebmasterWorld Administrator 5+ Year Member



 
Msg#: 4618855 posted 9:40 pm on Oct 24, 2013 (gmt 0)

Welcome to WebmasterWorld, cbo0485

Your example in the opening post, which is the code you are using, is a redirect (it has [R=301] at the end of the RewriteRule line).

Your example in your second post is a rewrite (as there is no [R=301] at the end of RewriteRule line).

So your second post rules did not "automate" the rules from the first post, it in fact also changed the method.

Whether you want redirect or rewrite depends on what URL you want the browser to show in the browser address bar once the user requests https://promoabc.com

If the address in browser should stay https://promoabc.com then you need a rewrite

If the address in browser should change to https://example.com/promoabc then you need a redirect

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4618855 posted 4:18 am on Oct 25, 2013 (gmt 0)

RewriteCond %{SERVER_NAME} !^(.*example\.com)
RewriteCond %{SERVER_NAME}= ^(.*)(.*\.com)

Normally I would wander off sobbing brokenly at this point, so it is just your bad luck that I have 28 pounds of cat preventing me from moving.

Since you're not capturing from the first condition-- in fact you couldn't if you wanted to-- there's no need for the parentheses. What you do need at this point, before the rule you're asking about, is canonicalization for example.com. That's the rule whose condition includes
!^(www\.example\.com)?$
--leaving off the www. if you prefer the "without" form. Then the rule covering all other domain names need only say
!example\.com
without anchors.

The crucial question is the one aak asked: Do you want your visitors to see the hostname they originally requested-- your assorted "vanity" domain names --or do you want everyone to end up in the same place?

If everyone is getting redirected, and no other sites live on this server-- that is, in an area where they will encounter this rule-- then you don't even need a separate rule. The canonicalization redirect by itself will get everyone onto the same page.

Are /abc/ /def/ /ghi/ and so on real, physical directories that match the assorted domain names? And every time you add a name, you create a new physical directory? If so, adding a line to the config file would seem to be the least of your problems.

Anyway-- this is what I started out to say, but got sidetracked-- a single non-final .* is bad enough. Two starts crossing the line from inefficient to dangerous.

!example\.com
(?:www\.)?(\w+)\.(?:com|org|net)

:: detour to figure out why it's SERVER_NAME instead of HTTP_HOST ::

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4618855 posted 6:49 am on Oct 25, 2013 (gmt 0)

Your example in your second post is a rewrite (as there is no [R=301] at the end of RewriteRule line).

That code is a redirect because the target includes a host name. Since there is no R flag, it's a 302 redirect.

Again the question, when user asks for promoabc.com should the browser address bar continue to show promoabc.com while content is fetched from the /promoabc folder (a rewrite) or should the browser address bar change to example.com/promoabc before the content is fetched (a redirect)?

cbo0485



 
Msg#: 4618855 posted 1:09 pm on Oct 25, 2013 (gmt 0)

It is a permanent re-write. We are using the R=301 to show it's a permanent site so it doesn't return a 302. But when they hit promoabc.com, this will re-write them so they see example.com/promoabc.

And yes our standard is to within htdocs create a directory name that matches the vanity URL. So if tomorrow they request a url called promotionabc.com, we will create a directory called promotionabc and include the files needed to display the page within there.

After some more testing here is where I am at now

RewriteCond %{SERVER_NAME} !^example\.com
RewriteCond %{SERVER_NAME}= ^www.(.*)(.*\.com)
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^/(.*) https://example.com/%1


g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4618855 posted 3:32 pm on Oct 25, 2013 (gmt 0)

I think you have misunderstood the terms "redirect" and "rewrite".

With a redirect the browser asks for a particular URL and the server replies with a 301 or 302 status code and suggests the browser asks for a different URL. The browser automatically asks for that new URL and the URL shown in the browser address bar changes to show that new URL.

With a rewrite, the browser asks for a particular URL and the mod_rewrite rules change the internal file pointer so that Apache fetches content from a different place on the hard drive than might have originally been expected.

it's a permanent re-write

The code you have posted is for a temporary redirect.

Why do you not want the user to stay on promoabc.com or promoxyz.com?

Once they arrive at example.com/promoabc is there navigation to other parts of example.com or do they just look at example.com/promoabc unaware of the rest of the pages on example.com?

[edited by: phranque at 12:23 am (utc) on Oct 26, 2013]
[edit reason] fixed typo [/edit]

cbo0485



 
Msg#: 4618855 posted 3:44 pm on Oct 25, 2013 (gmt 0)

There is navigation to other parts of example.com within that page.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4618855 posted 9:14 pm on Oct 25, 2013 (gmt 0)

OK. So what you need is a RewriteRule configured to deliver a 301 redirect.

You'll need the [R=301,L] flag on the end.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved