How do I format this htaccess rule to not redirect a specific query? - .htaccess

I am using an htaccess rule to redirect all urls in the format of:
www.example.com/blog/the-name-of-the-post
to
www.example.com/the-name-of-the-post
So I've removed "blog" from the URL. The redirect rule below is working. However I do not want to redirect any URLs in the format of:
www.example.com/blog/page/x
So if "/page" appears after "blog", then I don't want to do the redirect. The problem is I'm also redirecting the blog pages when paginated.
RedirectMatch 301 ^/blog/([^/]+)/([^/]+)/$ https://www.example.com/$2
In sum,
www.mysite.com/blog/the-name-of-post redirects to www.example.com/the-name-of-post
www.example.com/page/1 (or page/2, page/3, etc) do not redirect
Thanks

If you don't want to redirect when page appears after blog, you can do a negative match for the word page, like so:
RedirectMatch 302 /blog/(?!page).*$ https://www.example.com/
I'm not entirely clear on what all the different possibilities are from your description so I'm just giving you an example of not matching when the word page appears in the second position of the URI.

Related

URL Redirection if only certain URL is matched htaccess

I have a very small and odd issue. I want to write a rule which allows me to redirect URL.
https://www.example.com/category/florists/
to
https://www.example.com/category/florists.html
but to keep in mind there are other URLs which will be made from the above like
https://www.example.com/category/florists/fl/miami.html
I wrote a rule in .htaccess but it is causing trouble to later URLs
Redirect 302 "/category/florists/" /category/florists.html
this rule works fine but for this URL
https://www.example.com/category/florists/fl/miami.html
it makes it like this
https://www.example.com/category/florists.html/fl/miami.html
how can I solve it?
The mod_alias Redirect directive uses simple prefix-matching and everything after the match is copied into the end of the target URL (which explains your undesirable and malformed additional redirects).
To match just that one URL you need to use a RedirectMatch directive instead which matches against a regular expression (not prefix-matching).
For example:
RedirectMatch 302 ^/category/florists/$ /category/florists.html

Problem with redirection of shop subcategories in .htaccess

I couldn't find an answer, how to redirect certain subcategories...
I've old shop with paths like that:
shop.com/old_cat1/old_cat2/old_cat3
What I want, is to redirect it like that:
shop.com/new_cat2
shop.com/new_cat3
When I'm trying to use this code:
Redirect 301 /old_cat1/old_cat2/ https://shop.com/new_cat2
Redirect 301 /old_cat1/old_cat2/old_cat3 https://shop.com/new_cat3
redirection from old_cat2 to new_cat2 is working, but redirection to new_cat3 is sending me to new_cat2 (higher level). What should I do? I was trying with RedirectMatch, but it gave me nothing.

htaccess redirect with folder name

I need some help with htaccess redirects. Out site was wrongly crawled by google and as a result there are a lot of wrong urls being shown in webmaster tool. As an example:
articles/abcd/xyz
should be redirected as
articles/abcd/
articles/abcd/xyz.php
should be redirected as
articles/abcd/
articles/abcd/xyz.html
should be redirected as
articles/abcd/
So basically I am trying to mean always redirect to articles/abcd/ for varous wrong url types that i shown above. Please could you help
You can simply use RedirectMatch from mod_alias (documentation). We assume that the part after articles does not contain any / character. We redirect with a temporary redirect to the url without the suffix. Change "temp" to "permanent" after testing that this redirect actually works as you expect it to work.
RedirectMatch temp ^(/articles/[^/]+/).+$ $1

htaccess redirect with wildcard and not recursive

I've consolidated about 20 old pages into one new page, and want to redirect web links going to those pages to the new page.
I started out listing each one in htaccess as a Redirect 301, but thought I might save processing time to do a wildcard string match instead. Unfortunately it failed, because I suspect the page I want to go to is also caught in the wildcard.
For example I want to redirect, www.mydomain.com/catalog/listname_oranges.php, listname_lemons.php, listname_figs.php etc to redirect to www.mydomain.com/catalog/listname_addons.php
So I tried this, which failed:
RedirectMatch 301 ^/catalog/listname_.*$ /catalog/listname_addons.php
How do I fix this so its not recursive?
You can use a negative lookahead in your regex:
RedirectMatch 301 ^/catalog/listname_(?!addons\.php).*$ /catalog/listname_addons.php
This way, the listname_addons.php file won't match the regex but everything else will.

how to use htaccess to chance the URL...?

I googled and looked on stackover flow but i failed to really understand the answers
I;m not trying to REDIRECT but CHANGE the WAY the URLS LOOK
I want to change a these into the new urls
www.site.com/abc.php to www.site.com/
(my index page currently has my login page so I can't use the index.php)
www.site.com/abc.php#123.php to www.site.com/123.php
UPDATE:
Ok, how can I do this then
www.site.com/abc.php to www.site.com/abc/
so that when a hash link is present it looks like
www.site.com/abc/#123.pho
or if possible
www.site.com/abc/#123/
In addition to the other answer on redirects, if you want more advanced forwarding you can use the Apache RewriteEngine module.
You can then use regexps, which may include subpatterns.
Example from my site, I created the patterns after I've imported everything from blogger to wordpress. Whenever someone visits an URL like http://www.twistedmind.nu/2006_03_01_archive.html he'd be redirected to http://twistedmind.nu/2006/03
RewriteEngine on
RedirectMatch 301 (([0-9]*)_([0-9]*)_([0-9])(.)(.html))$ http://twistedmind.nu/$2/$3/
RedirectMatch 301 (([0-9])(.)(.html))$ http://twistedmind.nu/$1
Based on the other answer, you can't match on everything that's after the hash tag # though.
Other example (added after comment):
RedirectMatch 301 (.*).php$ http://www.mysite.com/$1
This should strip the .php extension from all links, the new link (withouth .php) should exist.
You can use mod_rewrite if you want to create 'virtual' urls that redirect something like mysample to mysample.php. See http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html for an explanation.
You can use an .htaccess only for the first url.
Redirect /abc.php http://www.site.com/
The second url cannot be redirected with the .htaccess.
You can use javascript for that:
<script type="text/javascript">
if (location.hash == "123")
location.href = location.hash+".php";
</script>

Resources