homepage Welcome to WebmasterWorld Guest from
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

Kindly assist writing an htaccess rule!

 2:57 am on Jan 26, 2014 (gmt 0)


I have some urls which are now 404 due to site upgrade.

I want all urls starting with http://www.example.com/archives/* to point to http://www.example.com/news

(notice that after archives there is a wildcard char)

Can someone here please help me with the code for htaccess rules? Thanks a lot in advance!




 3:27 am on Jan 26, 2014 (gmt 0)

ideally you want to redirect urls on a one-to-one basis to equivalent content.

what have you tried so far?


 3:33 am on Jan 26, 2014 (gmt 0)

I tried some code yesterday but it didn't work and now I don't remember (sorry!).

Can you assist? Thanks!


 4:00 am on Jan 26, 2014 (gmt 0)

it didn't work and now I don't remember

Well, then you can simply start from scratch. This time show us what you try, and make sure you say exactly what happens. "Didn't work" can mean a lot of different things.

Do you currently have any rules using mod_rewrite? (If you're using a CMS, the answer is yes.) If so, express your redirects as RewriteRule. Otherwise you can use mod_alias (Redirect by that name); this is the kind of redirect that it was made for.


 4:34 am on Jan 26, 2014 (gmt 0)

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com\archives$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com\archives$
rewriterule ^(.*)$ http://www.example.com [R=301,L]

Kindly assist.


 7:27 am on Jan 26, 2014 (gmt 0)

Ouch. You're doing this on purpose, aren't you? :)

#1 HTTP_HOST is only the part before the first slash. So it's example.com, or www.example.com, or possibly www.example.com:8080.

#2 Do you have more than one site (including subdomains) passing through the same htaccess file? If no, you don't need to say anything about the host at all. It will all get regularized in the target of the rule.

#3 In mod_rewrite (and anything else using Regular Expressions), a backslash is the escape character. Directory slashes / look the same as they do in your browser. Possibly you use windows in real life?

#4 No point in capturing if you're not going to reuse the captured material. Here you do need to reuse the captured part-- but only the part after /archives/. This is a key difference between mod_rewrite and mod_alias: nothing gets reappended by default. Yes, OK, the query string does. But that's not at issue here.

#5 Where is this rule located? Ordinarily you'd want it in the root. (Invoking mod_rewrite more than once along the same path is generally not a good idea. Do as I say, not as I do.) That means the pattern will start


while the target will start



 4:30 am on Jan 27, 2014 (gmt 0)

I've checked it also, it is working. Today again I've checked it's working.

I've placed this rewrite directive on <snip>

Try entering this url in browser:


Here is the another code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com/archives [NC]
RewriteRule ^archives/(.*) http://example.com/news [R=301,L]

[edited by: incrediBILL at 5:20 am (utc) on Jan 27, 2014]
[edit reason] URls removed. Please see forum charter and TOS [/edit]


 4:53 am on Jan 27, 2014 (gmt 0)

Hi Rag, it didn't work (the htaccess code). It still redirects the page to 404.


 6:25 am on Jan 27, 2014 (gmt 0)

You need to paste your .htaccess so that we can have a look.

Also try moving this code in the very top or to bottom and see if it works


 6:55 am on Jan 27, 2014 (gmt 0)

RewriteRule ^archives/(.*) http://example.com/news [R=301,L]

You need to reuse the captured part of the path. Otherwise everyone's just getting redirected to example.com/news and that's all.


 2:31 pm on Jan 27, 2014 (gmt 0)

Dear Lucy, could you please assist and give me a correct version of the code? :-)

I'm not a programmer or avid Apache user.



 2:37 pm on Jan 27, 2014 (gmt 0)

He himself does not know what he wants. I'm unsubscribing ..


 5:12 pm on Jan 27, 2014 (gmt 0)

He knows what he wants, he just hasn't spent nine years making ebooks, leading to relative fluency in Regular Expressions. (I am not kidding when I say I don't speak a word of Apache. Here you don't need it.)

exhibit A:
RewriteRule ^archives/(.*) http://example.com/news [R=301,L]

exhibit B:
RewriteRule ^archives/(.*) http://example.com/news/$1 [R=301,L]

See the difference? Try each one in your browser and you will see it in your address bar.


 6:13 am on Jan 28, 2014 (gmt 0)

Hi, the other stuff remains the same? that is,

RewriteCond %{HTTP_HOST} ^example.com\archives$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com\archives$

Thanks a lot!


 6:32 am on Jan 28, 2014 (gmt 0)

You should have a separate rule in place that takes care of redirection either www. or non www. version to the other. That should not be part of this redirect.


 9:23 am on Jan 28, 2014 (gmt 0)

the other stuff remains the same? that is,

RewriteCond %{HTTP_HOST} ^example.com\archives$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com\archives$

Oh my ###. I thought we'd got rid of the HTTP_HOST business ages ago. The host is never example.com/anything-here, so the conditions will always fail and the rule will never execute. See msg4640259 halfway up this thread.

:: pause as I now understand why the same person* seems to be posting twice ::

* [a-z]{3,4}2[a-z]{3,4}


 4:53 pm on Feb 1, 2014 (gmt 0)

The following message was cut out to new thread by incredibill. New thread at: apache/4641806.htm [webmasterworld.com]
3:49 pm on Feb 1, 2014 (PST -8)

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