.htaccess redirect with multiple hypens in url - .htaccess

we have move our website and decided to remove all the old news, if is there a way to redirect urls which contain '-'
here is an example of some fo the URLs we have redirect.
RedirectMatch 301 /climate-change-threatens-expose-cold-war-secrets/?$ /news
RedirectMatch 301 /countdown-to-armed-forces-day-begins-in-liverpool/?$ /news
RedirectMatch 301 /chilcot-report-the-key-findings/?$ /news
RedirectMatch 301 /cold-war-nuclear-base-uncovered-ice-melts/?$ /news
RedirectMatch 301 /corbyn-refuses-guarantee-support-nato-allies/?$ /news
RedirectMatch 301 /comment-if-were-in-a-cold-war-how-do-we-get-out/?$ /news
Is it possible to redirect anything with along these lines "word1-word2-word3-OrEvenMoreWords"
Thanks

If there are at least 2 words separated by - per news post then the following should be sufficient:
RedirectMatch 301 ^[^\-]+(?:\-[^\-]+)+$ /news # matches "my-post" or "my-long-post" but not "mypost" or "my--post"
^ matches the start of the URL.
[^\-] matches all except for -.
+ keeps matching the previous rule and ensures at least one is matched.
(?: begins a non-capturing group.
\- matches the - character.
[^\-] matches all except for -.
+ keeps matching the previous rule and ensures at least one is matched.
) ends the non-capturing group.
+ keeps repeating the non-capturing group ensuring it is matched at least once - the group begins with - and matches at least one character up to the next - so in this way we allow a-b-c-d...
$ matches the end of the URL, ensuring nothing remains unparsed.

Related

Page Redirect 301 using htaccess

I want to redirect /event/path1/{id}/{some_name} to /event/path2/{id},
I want to redirect old page to the new one and remove the last segment of the url,
I have done with this but not removing the last segment of the url, this is what .htaccess
RewriteRule ^event/path1/(.*) /event/path2/$1 [L,R=301]
You're using .* in your regex that matches everything after /event/path1/ thus resulting in wrong target URL.
You may use:
RewriteRule ^event/path1/([^/]+)/[^/]+/?$ /event/path2/$1? [L,R=301,NC]
Here [^/]+ will match 1 or more of any non-slash character.
? at the end of the target will strip off any previous query string.

htaccess redirect 301 with or without trailing slash

I have some urls looking like this:
redirect 301 /some/old/url/ http://example.com/url
redirect 301 /some/old/test/ http://example.com/test
What I do want is kind of like this:
redirect 301 /some/old/[word-match-with-or-without-trailing-slash] http://example.com/[same-word-without-trailing slash]
How do I do this? The problems:
Match both with or without slash.
Keep the match without slash for later use (see next).
Put the match witout slash at the end of the line.
Update
It seems like I could do something like this (untested):
redirect 301 /some/old/(.*) http://example.com/$1
But then how can I remove the possible trailing slash in $1?
To redirect to remove the trailing slash ,you can use :
RedirectMatch ^/some/old/(.*?)/?$ http://example.com/$1

Remove .htaccess 301 RedirectMatch trailing slash?

I have a redirectMatch rule in place:
RedirectMatch 301 /dir/subdir/safety-program(.*) /dir/subdir/safety-program/$1
When I load the safety-program address, like so:
http://localhost/dir/subdir/saftey-program/
or
http://localhost/dir/subdir/saftey-program
I get the safety-program page returned with the following address, with an extra trailing slash:
http://localhost/dir/subdir/safety-program//
Why is the match adding that second slash? How do I remove it?
the problem is here safety-program/$1 and it should be like this safety-program$1
why you should change it
the RedirectMatch comes like this
RedirectMatch [status] regex URL
So the [status] is: 301 and regex is : /dir/subdir/safety-program(.*) then there is a url which is : /dir/subdir/safety-program/$1 and this part, $1, of url will match this part ,(.*), of regex , so you put slash in this (.*) then it comes to url like this $1 including that slash , and you expressed about it like this /$1 in url , so that why double slash occurred.
That what you did exactly.
Whenever request /dir/subdir/safety-program/ it would be /dir/subdir/safety-program//
so change the line code to be :
RedirectMatch 301 /dir/subdir/safety-program(.*) /dir/subdir/safety-program$1
OR this :
RedirectMatch 301 /dir/subdir/safety-program/(.*) /dir/subdir/safety-program/$1

301 redirect only the contents of a subfolder and not the subfolder itself

I am trying to work out how to 301 redirect /hall-of-fame/(something-here) only (to /profile/(something-here)), and not redirect if somebody lands on the actual /hall-of-fame/ path.
So far I have:
RedirectMatch 301 ^/hall-of-fame/(.*)$ /profile/$1
However this also matches /hall-of-fame/ and redirects that to /profile/ - which I do not want.
Any suggestions?
Thanks
You need to change your regex just a little bit. If you use (.*) that means 0 of more characters, so it will also match an empty URI. You need to change it to (.+) meaning 1 or more. So there will have to be something after /hall-of-fame before it will redirect.
RedirectMatch 301 ^/hall-of-fame/(.+)$ /profile/$1

Remove trailing slash from URL/folder but do not redirect subfolders

I'm using mod_rewrite. The .htaccess contains this line:
# redirect old references
Redirect 301 /folder/ http://www.donain.de/folder
This works fine execpt for one thing: All requests to subfolders are redirected as well. For example: /folder/hello is redirected to /folder. How can I prevent this?
I know that I can determine the start of an expression using ^ in a .htaccess file. But I can't figure out to say "stop this rule there".
You need to use RedirectMatch for regex support:
RedirectMatch 301 ^/folder/?$ http://www.donain.de/folder

Resources