Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#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
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
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:
- turns mod_rewrite on (omit if already present in the used context)
- matches the hostname and captures the content following the subdomain
- redirects requested URIs from /quick-order/ directory to new location
%1
returns the domain name captured in the previous RewriteCond$0
the whole requested URI matched in theRewriteRule
- 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
.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^cdn\.(.*)$ [NC]
RewriteRule ^/?quick-order(/.*)?$ http://www.%1/$1 [R=301,QSA,L]
Offline
Offline
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).
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