Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2014-05-05 01:22:52

andrewstolpe
New Member
Registered: 2014-05-05
Posts: 3

Help with a htaccess rewrite rule

I was hoping you guys could help me out with a quick rewrite rule that has been troubling me for hours. I’m trying to make the following rewrite

http://cdn.domain.com/quick-order/category
go to
http://www.domain.com/quick-order/category

It needs to look for the subdomain (cdn.) and then find the /quick-order/ folder and then redirect to the corresponding www version. I can’t just have a complete cdn. -> www. redirect because I host my images on the CDN and it must also check for the folder of /quick-order/ because there are other folders and files on the CDN as well.

Thanks so much.

Offline

#2 2014-05-05 02:02:01

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Help with a htaccess rewrite rule

Using mod_alias:

Redirect 301 /quick-order http://www.domain.com/quick-order

Offline

#3 2014-05-05 02:28:40

andrewstolpe
New Member
Registered: 2014-05-05
Posts: 3

Re: Help with a htaccess rewrite rule

Hello Gocom,

I want to first thank you for taking the time to respond to my comment. I tried that previously and it creates a redirect loop for anyone who would visit http://www.quick-order/category-name at the start (it matches the first /quick-order part of the rule and will go to itself). I need to first do a check that the subdomain host is cdn.domain.com before that redirect rule is executed. I was thinking something like:

RewriteCond %{HTTP_HOST} ^cdn\.domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/quick-order/ [NC]
RewriteRule ^quick-order$ http://www.domain.com/quick-order/$1 [R=301,L]

I have no idea exactly what the above does but I’m trying to piece together parts that I find from various sources. Any ideas?

Thanks again,
Andrew

{Added Textile’s bc. for combing spaghetti. – Uli}

Last edited by uli (2014-05-05 10:22:19)

Offline

#4 2014-05-05 05:09:22

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Help with a htaccess rewrite rule

You should ultimately separate the two VirtualHosts. Otherwise both would be serving the same, duplicated content. But, something like this should work:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^cdn\.(.*)
RewriteRule ^/?quick-order(/.*)?$ http://www.%1/$0 [R=301,QSA,L]

The above:

  1. turns mod_rewrite on (omit if already present in the used context)
  2. matches the hostname and captures the content following the subdomain
  3. redirects requested URIs from /quick-order/ directory to new location
    1. %1 returns the domain name captured in the previous RewriteCond
    2. $0 the whole requested URI matched in the RewriteRule
    3. Flags R sets it to do redirect (301 being the HTTP status), QSA makes sure it appends query string to the redirected location and L ends redirect processing

For more information, see mod_rewrite and PCRE.

Offline

#5 2014-05-08 00:10:49

andrewstolpe
New Member
Registered: 2014-05-05
Posts: 3

Re: Help with a htaccess rewrite rule

This doesn’t seem to work either for some reason. Do you have any ideas why? I copied exactly as you posted it and put it at the top of my htaccess file. I’ve been playing with it for a few hours changing different parts and nothing seems to be working. Thank you again and I appreciate the help.

Offline

#6 2014-05-08 01:27:30

GugUser
Member
From: Quito (Ecuador)
Registered: 2007-12-16
Posts: 1,473

Re: Help with a htaccess rewrite rule

I think $0 should be $1.

What if you change to:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^cdn\.(.*)$ [NC]
RewriteRule ^/?quick-order(/.*)?$ http://www.%1/$1 [R=301,QSA,L]

Offline

#7 2014-05-08 08:47:40

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Help with a htaccess rewrite rule

GugUser wrote #280655:

I think $0 should be $1.

I don’t even… $0 returns the whole matched URI, $1 – $9 returns a single capture group, which in that context leads in to wrong target URL.

Offline

#8 2014-05-08 11:22:29

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: Help with a htaccess rewrite rule

andrewstolpe wrote #280654:

This doesn’t seem to work either for some reason. Do you have any ideas why? I copied exactly as you posted it and put it at the top of my htaccess file. I’ve been playing with it for a few hours changing different parts and nothing seems to be working. Thank you again and I appreciate the help.

Could you share the full contents of your .htaccess file?

You may want to try starting from something that works (ie. a simpler, hardcoded rewrite rule) and continue by replacing the hardcoded bits with grouping/capturing and captured values, one at a time, testing on each change… until you break it or until it works :)
(You would probably arrive to some solution similar —if not equal— to Gocom’s suggestions, which looks fine to me).


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#9 2014-05-12 20:18:36

GugUser
Member
From: Quito (Ecuador)
Registered: 2007-12-16
Posts: 1,473

Re: Help with a htaccess rewrite rule

@andrewstolpe:

I think it would be nice if you would still provide feedback.

Offline

Board footer

Powered by FluxBB