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.
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
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.
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?
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
are you using any RewriteRule directives in your server config or .htaccess?
do you really want a 302 or is that a Permanent move?
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.
If you have any RewriteRule directives, do not use any Redirect or RedirectMatch directives.
Change everything to use RewriteRule syntax.
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.
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:
The period has special meaning in regular expressions, so we should escape it with a backslash in order to match a literal period.
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.
And finally, we need to "remember" the portion of the URL up to the -sales part. In regular expressions, we use "capturing parentheses."
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.
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.
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.
|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