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

    
Redirect with wildcard not working
denisl

10+ Year Member



 
Msg#: 4604076 posted 10:24 am on Aug 22, 2013 (gmt 0)

Having decided to combine my widget-sales.php pages with my widget.php pages, with my limited knowledge I was expecting this to work and it doesnt:

redirect 301 /*/*/*-sales.php http://www.example.com/*/*/*.php

Is this not as easy as I hoped?

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4604076 posted 11:18 am on Aug 22, 2013 (gmt 0)

Super-short answer because it's 4 AM my time:

* in a target doesn't mean anything. It's a literal asterisk.

* in a pattern may have meaning as part of a Regular Expression. But that's only relevant if you're capturing.

Are you redirecting every single widget-sales.php page to a widget.php page of the same name? Use the same code that you've probably already got in place to canonicalize your domain name.

Tune in later for an explanation of the difference between mod_alias and mod_rewrite. You should be prepared to answer some basic questions, like whether the two domains live on the same server and whether you're doing this in htaccess or in config. Oh, and whether the /*/*/*-sales.php means that you're only redirecting requests for URLs following a particular pattern. That will probably be the most important question.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4604076 posted 5:49 pm on Aug 22, 2013 (gmt 0)

Whatever you do, I'll suggest that using a
RewriteRule with the [R=301,L] flags will probably do what you want, rather than using Redirect.

You'll need to be very specific about the pattern of URL paths that should be redirected and what the new URL looks like.

As ever, use example.com in all examples to prevent the URL auto-linking function.

denisl

10+ Year Member



 
Msg#: 4604076 posted 8:27 pm on Aug 22, 2013 (gmt 0)

Ok - I can see have lot to learn about using htaccess - I shall continue reading, but thank you for your comments.
Lucy, I see where your username comes from - at the computer 24hrs a day?

denisl

10+ Year Member



 
Msg#: 4604076 posted 1:32 pm on Aug 23, 2013 (gmt 0)

Got a bit deep and out f my depth, trying a few things that didnt work, and being very nervous about meddling.
I then cam across something I was able to midify and use to produce this:
RedirectMatch 302 (.*)-sales\.php$ $1.php

This appears to do just what I wanted

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4604076 posted 8:44 pm on Aug 23, 2013 (gmt 0)

are you using any RewriteRule directives in your server config or .htaccess?

do you really want a 302 or is that a Permanent move?

denisl

10+ Year Member



 
Msg#: 4604076 posted 9:19 pm on Aug 23, 2013 (gmt 0)

Yes I am using some RewriteRules in htaccess.

The 302 is just me being careful until I am sure that I am only redirecting where required.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4604076 posted 12:11 am on Aug 24, 2013 (gmt 0)

If you have any RewriteRule directives, do not use any Redirect or RedirectMatch directives.

Change everything to use RewriteRule syntax.

Dideved



 
Msg#: 4604076 posted 12:27 am on Aug 24, 2013 (gmt 0)

I thought I'd come by and add some explanations to the imperatives everyone is handing out.

@denisl Typically we expect code to be processed in a top-to-bottom, left-to-right order, but that's not always the case with Apache configurations. Apache will evaluate all rewrite directives first and all redirect directives second, even if the redirects appear first in your code. Due to this non-obvious execution order, it's probably better to use _only_ Redirect or _only_ RewriteRule. That way things will execute in an obvious top-down order.

Dideved



 
Msg#: 4604076 posted 12:56 am on Aug 24, 2013 (gmt 0)

/*/*/*-sales.php


It looks like you're trying to write using a globbing [en.wikipedia.org] style of pattern matching. But Apache uses a Perl-compatible regular expression style of pattern matching. In globbing, I think the * means any filename or directory. The equivalent regular expression would be:

[^/]+

This means one or more non-slash characters. So now your pattern looks like this:

/[^/]+/[^/]+/[^/]+-sales.php

The period has special meaning in regular expressions, so we should escape it with a backslash in order to match a literal period.

/[^/]+/[^/]+/[^/]+-sales\.php

We also need to use start- and end-of-string anchors. Right now, this URL pattern we're matching could appear somewhere in the middle of a much larger string that we don't want to match. We need to make sure that our pattern can only start matching right at the beginning of the string and can only stop matching at the end.

^/[^/]+/[^/]+/[^/]+-sales\.php$

And finally, we need to "remember" the portion of the URL up to the -sales part. In regular expressions, we use "capturing parentheses."

^(/[^/]+/[^/]+/[^/]+)-sales\.php$

Now that portion will be available as the variable $1 when we form the replacement URL.

(Note that if we use the redirect directive, then we need to match the initial slash in the path, but if we use a rewrite directive, then in most cases, we must not match the leading slash. In this case, it's probably best to switch to to a rewrite rule.)

Here's the finished product:

RewriteRule ^([^/]+/[^/]+/[^/]+)-sales\.php$ $1.php [R=302,L]

Switch the R=302 to R=301 whenever you feel comfortable.

denisl

10+ Year Member



 
Msg#: 4604076 posted 8:49 am on Aug 24, 2013 (gmt 0)

Well i know a lot more than I did a few days ago - thank you everyone.
@Dideved Thank you for that - I had been working along those lines but when it didn't work, looked for an alternative.

I have noted the comments on combining redirects and rewrites but can't see it as a problem at the moment - I have several other individual pages that also use redirect rule.

No doubt in the future I will feel more confident and tidy it all up.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4604076 posted 3:03 pm on Aug 24, 2013 (gmt 0)

If rules run in a different order to what you expect, internally rewritten paths can be exposed back out on to the web as new URLs. That's often a disaster.

If you have any RewriteRule directives anywhere at all within your site configuration, you must find all of the Redirect and RedirectMatch directives and change them to also use RewriteRule syntax.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4604076 posted 8:45 pm on Aug 27, 2013 (gmt 0)

http://httpd.apache.org/docs/current/rewrite/avoid.html [httpd.apache.org]:
The use of RewriteRule to perform this task may be appropriate if there are other RewriteRule directives in the same scope. This is because, when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file.


with mod_alias, you can only send external redirects and you can only match url paths.
this means if you are doing any of the following, you are probably using mod_rewrite (RewriteRule) and should avoid using mod_alias (Redirect):
- an internal rewrite
- a redirect based on the query string matching
- a redirect based on hostname matching

some of the ill effects from this situation include:
- exposure of (internal) rewritten urls in a subsequent (external) mod_alias Redirect
- a url gets redirected with a more general mod_rewrite redirect instead of firing the more specific mod_alias redirect
- multiple redirect hops to reach the canonical url

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