Forum Moderators: phranque

Message Too Old, No Replies

301 filename+wildcard.html to example.com

static file name variable 301 syntax

         

tzd123

5:37 am on Nov 30, 2011 (gmt 0)

10+ Year Member



Hi everyone. Im in a bind. I know how to do regular 301s, however,there is a type of 301 ive been having difficulty figuring out.


Say I want to 301...

example.com/abc-xyz.htm to example.com

...where abc can be any combination of letters/characters of whatever length. What i want to do is 301 redir anything that starts with example.com/abc and that ends in whatever .htm, to the root domain of the website.

I appreciate the help.

thanks,

wilderness

6:06 am on Nov 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Forum Library [webmasterworld.com]

24. Mod_Rewrite & Regular Expressions [webmasterworld.com]


letter-letter (EG [a-z] matches any single lowercase alphabetical character in the range of a to z), so [c-e] will match any single character that is the lowercase letter c, d, or e.

LETTER-LETTER (EG [A-Z] matches any single capital alphabetical character in the range of A to Z), so [C-E] will match any single character that is the capital letter C, D, or E.

number-number (EG [0-9] matches any single number in the range of 0 to 9), so [4-6] would match any single number 4, 5, or 6.

lucy24

8:26 am on Nov 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If abc can be "any combination of letters/characters of whatever length" then what combination or permutation do you not want to redirect? Is the hyphen significant? You didn't say anything about it.

I don't know if \w works in all Apache installations, but it works fine for me.

tzd123

3:01 pm on Nov 30, 2011 (gmt 0)

10+ Year Member



Thank wilderness and lucy24 for your replies.

>then what combination or permutation do you not want to redirect?
I want to redirect all of them so long as the file name starts with abc- and is in the root directory.

ie,

example.com/abc-xyz.htm
example.com/abc-swe.htm
example.com/abc-ccx.htm
example.com/abc-ew3.htm
example.com/abc-76g.htm

etc...

I was hoping i could do a one liner instead of 301ing line by line...

RedirectMatch 301 /abc-*.htm http://example.com/$1

...where * is a wildcard that could be any alpha numerical string, but that obviously doesnt work.


Thanks again for your help.

g1smd

8:05 pm on Nov 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need a RewriteRule and it will use a simple Regular Expression pattern to match "/abc-" followed by "not a period one or more times" followed by ".htm". Your pattern will not use .* at all.

tzd123

10:16 pm on Nov 30, 2011 (gmt 0)

10+ Year Member



thanks, but I dont want to rewrite the url, i want to redirect them elsewhere.

lucy24

10:18 pm on Nov 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh, OK, so when you said "abc can be..." you really mean "xyz can be..." That is, you've got a piece of literal text that you're matching.

Your pattern will not use .* at all.

And it will also not use www.example.com at all, because that part is assumed in mod_rewrite. Riffle through a few random threads in this Forum and you will see what to use instead of .*

Although come to think of it... Do you even have non-htm files that begin with /abc? If not, you don't need to constrain the rule.

tzd123

10:39 pm on Nov 30, 2011 (gmt 0)

10+ Year Member



yeah, abc- doesnt change, xyz does.

>Although come to think of it... Do you even have non-htm files that begin with /abc?

I do not.

>If not, you don't need to constrain the rule.

how would the redir syntax look like? Ive tried several permutations using apache's regular expressions but couldnt get the desired effect.

g1smd

1:24 am on Dec 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I dont want to rewrite the url

You can use RewriteRule to generate an external redirect or to configure an internal rewrite. It's all down to how you code it.

Let's see what you tried. You need a RewriteRule and it will use a simple Regular Expression pattern to match "/abc-" followed by "not a period one or more times" followed by ".htm". Your pattern will not use .* at all.

lucy24

5:06 am on Dec 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



"not a period one or more times" followed by ".htm". Your pattern will not use .* at all.

The previous round of chitchat established that he doesn't need the .htm because there aren't any non-.htm files that start in /abc. This in turn means that it doesn't matter about the stuff that comes after /abc, making the whole pattern vastly easier. But sorry, tzd, we're still going to sit back and make you do it yourself. And then you can go around crowing that you made an .htaccess directive all by yourself ;)

wilderness

6:45 am on Dec 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



a site search on quantifiers [google.com] will also provide examples

lucy24

9:06 am on Dec 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh, and everyone including me has consistently been saying one thing that is not literally true and will lead you astray. Let's see if you can figure it out ;)

tzd123

5:03 pm on Dec 1, 2011 (gmt 0)

10+ Year Member



Got it. I didnt use a rewrite, i used a straight up RedirectMatch 301. g1smd comment of "Your pattern will not use .* at all" was key. I went to my original try, removed the .* and viola.

>Oh, and everyone including me has consistently been saying
>one thing that is not literally true and will lead you
>astray.

Why do that?

g1smd

7:14 pm on Dec 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need a RewriteRule configured to deliver an external redirect and not to preform an internal rewrite.

Don't use Redirect or RedirectMatch.

lucy24

8:29 pm on Dec 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why do that?

We're not doing it on purpose. I just noticed it as I looked over the thread :) It's one of the subtle differences in wording between mod_alias (Redirect by that name) and mod_rewrite (Redirect via R=301 flag).

tzd123

9:10 pm on Dec 2, 2011 (gmt 0)

10+ Year Member



@lucy24, oh ok... i thought the inverse.

@g1smd why not use RedirectMatch?

I used RedirectMatch 301 /abc http://example.com/ and it worked fine. Is there a pitfall? or reason why I shouldnt?

Thanks

wilderness

9:17 pm on Dec 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



why not use RedirectMatch?


Because the mixing of mod_alias and mod_rewrite exposes rewrites and/or redirects in the URL Path in a way of which you may not control and/or anticipate.

g1smd

9:18 pm on Dec 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Once you add other rules using RewriteRule in your .htaccess file you'll never be sure which order the rules are being parsed.

Use RewriteRule for everything and sleep easy.