homepage Welcome to WebmasterWorld Guest from 50.19.33.5
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
redirect files with certain pattern to directory
eddierosenthal




msg:4583112
 2:03 pm on Jun 11, 2013 (gmt 0)

Hi - this is my first post here, i think.
I moved a site that had about 2500 static files in the root but rebuilt the site using wordpress, and put those files out of the root and put them instead in a directory /archive
The files are all of a pattern like this example file name a1356973200.htm where the a13 seems to be the beginning of the pattern.
so i need to redirect all requests for the files
domain.com/a1356973200.htm to domain.com/archive/a1356973200.htm for all files matching a pattern.
suggestions welcome! thanks.
ed

 

phranque




msg:4583132
 2:41 pm on Jun 11, 2013 (gmt 0)

welcome to WebmasterWorld, ed!


actually you posted once before.
.htaccess works in ff and safari, fails in ie and chrome:
http://www.webmasterworld.com/apache/4434013.htm [webmasterworld.com]


you should use the mod_rewrite module for this.
the Pattern string of the RewriteRule directive should capture the part of the path you want to reuse as a backreference ($N) in the Substitution string.

this thread should give you a place to start - How to redirect domain to a subfolder:
http://www.webmasterworld.com/apache/3621067.htm [webmasterworld.com]

eddierosenthal




msg:4583140
 3:16 pm on Jun 11, 2013 (gmt 0)

ok i stand corrected. i do that a lot.
in my case wordpress has its own little .htaccess file - should code i write go above or below that? experimenting will create mayhem.
I will have to experiment on a separate domain in a test case to see if i can find the solution. testing on a live site i can see will be somewhat detrimental.
the pattern i will want will look like this most likely
([a13]+.*)$ yes?
so the rewrite rule would look like this

RewriteCond %{HTTP_HOST} ^([a13]+.*)$
RewriteRule ^(.*)$ http://www.example.com/archives/$1 [L,R=301]

agreed or no?
thanks for your reply!

[edited by: phranque at 3:34 pm (utc) on Jun 11, 2013]
[edit reason] use example.com please [/edit]

phranque




msg:4583152
 3:31 pm on Jun 11, 2013 (gmt 0)

[a13] is a character class which means "a or 1 or 3" and [a13]+ means one or more of those 3 characters, which isn't really what you want.
assuming all numeric characters between the "a" and the ".htm", you could use a character class for those characters.
so try something more like:
^(a13[0-9]+\.htm)$
and you want to use this regular expression as the Pattern of the RewriteRule.

i'm not sure why you are testing %{HTTP_HOST} for anything as that wasn't in your problem statement.

eddierosenthal




msg:4583158
 4:07 pm on Jun 11, 2013 (gmt 0)

i guess i am not sure - ok i am sure i don't know.
i set up a directory archives under an unused wp install,
so i have some sort of testing environment.
i put a1370365200.htm in that archives dir.

i tried this

RewriteEngine On
RewriteRule ^/a13[0-9]+\.htm)$ http://www.example.com/responsive/archives/$1 [R=301,NC,L]
but i get page not found
when i looked for
http://example.com/responsive/a1370365200.htm
where that file is one that i put in the archives directory.
thanks for your replies...

[edited by: phranque at 9:56 pm (utc) on Jun 11, 2013]
[edit reason] [webmasterworld.com...] [/edit]

eddierosenthal




msg:4583168
 4:18 pm on Jun 11, 2013 (gmt 0)

when i look at the urls i wrote they are all enclosed in brackets with ellipsis. can you actually see what it is i am trying to indicate?

phranque




msg:4583282
 10:02 pm on Jun 11, 2013 (gmt 0)

the forum software automatically links urls.
please use example.com.
[webmasterworld.com...]

you didn't mention anything about a subdirectory, so you will have to capture that in the Pattern for the RewriteRule.
is that a fixed or variable path before the file name?

you are also missing the opening parenthesis of the capture group in that pattern.

lucy24




msg:4583290
 10:54 pm on Jun 11, 2013 (gmt 0)

in my case wordpress has its own little .htaccess file - should code i write go above or below that?

You should put each rule where it logically belongs, regardless of who wrote it or when it was added.

Within the past couple of days I posted a pretty detailed explanation of how RewriteRules should be ordered which I now can't find, so possibly it wasn't in /apache/ as I thought* so I won't repeat it here. Short version: lockouts before redirects, redirects before rewrites.

Unless you are absolutely certain you know what you are doing, do not repeat DO NOT put RewriteRules in more than one htaccess file on the same path. That is:

(a)
/directory/.htaccess
(b)
/directory/subdir1/.htaccess
/directory/subdir2/.htaccess

You can EITHER put RewriteRules in (a) only, OR put them in one or both of (b). And, in the second case, "both" is only safe if /subdir1/ and /subdir2/ never redirect or rewrite to each other.


* Punch line: The whole time I was going bonkers with site search looking for everything under the sun-- and utterly failing to find it-- the very post I was searching for was open in the immediately adjacent tab:

[webmasterworld.com...]

memo to self: there is only one "t" in "highlight"

eddierosenthal




msg:4583331
 3:51 am on Jun 12, 2013 (gmt 0)

i did solve this and thanks for your help.
here was what i did, substituting example.com in place of the domain name
RewriteEngine on
RewriteRule ^(a[0-9]+\.htm)$ http://example.com/archives/$1 [R=301,L]
RewriteRule ^(more[0-9]\.htm)$ http://example.com/archives/$1 [R=301,L]
wp htaccess here...
so you did help, by giving me the idea i needed to get me going...
thanks! ed

phranque




msg:4583337
 4:26 am on Jun 12, 2013 (gmt 0)

^(more[0-9]\.htm)$


you are only going to match a single numeric digit after "more" in that regular expression.
is that what you want?

eddierosenthal




msg:4583338
 4:40 am on Jun 12, 2013 (gmt 0)

Yes. there are 9 more files more0-more9

eddierosenthal




msg:4583339
 4:40 am on Jun 12, 2013 (gmt 0)

i mean more0 - more8. 9 files.

phranque




msg:4583362
 8:12 am on Jun 12, 2013 (gmt 0)

i mean more0 - more8. 9 files.

if you want to be precise, use this:
^(more[0-8]\.htm)$


i'm guessing you don't have any hostname canonicalization in place, which may be necessary depending on your configuration.
for example, what happens when you request these urls?
http://example.com/more0.htm
http://www.example.com/more0.htm

the non-canonical request should be (301) redirected to the new path on the canonical hostname in one hop.


you might also want to read this.
How To Speed Up WordPress:
http://www.webmasterworld.com/apache/4502109.htm [webmasterworld.com]

lucy24




msg:4583384
 9:07 am on Jun 12, 2013 (gmt 0)

phranque, have you got multiple tabs open again? ;)

Most recently posted rule:
RewriteRule ^(a[0-9]+\.htm)$ http://example.com/archives/$1 [R=301,L]
RewriteRule ^(more[0-9]\.htm)$ http://example.com/archives/$1 [R=301,L]

Looks pretty canonical to me :)

phranque




msg:4583399
 10:48 am on Jun 12, 2013 (gmt 0)

given that this was one of the OP's example rules:
RewriteRule ^(.*)$ http://www.example.com/archives/$1 [L,R=301]

i wouldn't make any assumptions that the rules most recently posted use a canonical hostname.

and neither of the rules most recently posted would handle requests for http://example.com/ vs http://www.example.com/ so i thought it would be prudent to ask.

if the generic hostname canonicalization rule does exist and www.example.com happens to be the canonical hostname then we are looking at a minimum chain of 2 redirects if the request is for http://example.com/more0.htm given the rules most recently posted.

lucy24




msg:4583571
 7:51 pm on Jun 12, 2013 (gmt 0)

neither of the rules most recently posted would handle requests for http://example.com/ vs http://www.example.com/

I don't understand why not, unless a preceding RewriteCond got edited out.

eddierosenthal




msg:4583587
 8:56 pm on Jun 12, 2013 (gmt 0)

i just tested both www and non www and they both worked ok.

phranque




msg:4583603
 9:38 pm on Jun 12, 2013 (gmt 0)

they both worked ok


one of them should get a 200 OK and the other should get a 301.

phranque




msg:4583608
 9:40 pm on Jun 12, 2013 (gmt 0)

I don't understand why not


none of the rules discussed so far would affect requests for the document root directory.

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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved