How to redirect urls starting with numbers - .htaccess

What will be a 301 redirect rule using htaccess to redirect urls similar to
domain.com/community/783-anystring-string/profile
to
domain.com/community/
or
any url matching this type of format
domain.com/community/123-anystring-string/...
to
domain.com/community/
Basically I want to redirect any urls domain.com/community/(starting with numbers)-string...
to domain.com/community/

You'll want to use mod_rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^community/[0-9]* /community [R=301,L]
I think. That may not work, I can't test it right now :/

Related

htaccess redirect all pages ending with .html to without html Except some pages

I'm having an issue with my .htaccess rules for redirection.
I need to redirect pages ending with .html to the pages ending with slash.
/about-us.html to /about-us/
But there are also pages where I need to redirect to specific page without .html like
/gallery/first-gallery/this-is-the-gallery.html to /gallery/new/
The problem is that my rules are not working together nicely, it seems that the rule to redirect .html pages ignores all other rules.
This is how my .htaccess looks like
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?(.*).(html)$ /$1 [R=301,L]
Redirect 301 /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131173-58e29f485064eprinceton-texas-roofing-services-1.html /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/
Redirect 301 /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131174-58e2a063b8b06princeton-texas-roofing-services-2.html /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/
#Many other 301 rules below
</IfModule>
So when I type in domain.com/about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131173-58e29f485064eprinceton-texas-roofing-services-1.html in the browser it redirects to domain.com/about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131173-58e29f485064eprinceton-texas-roofing-services-1 where I need this specific redirect to redirect to /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/
I tried moving the RewriteRule ^/?(.*).(html)$ /$1 [R=301,L] to the bottom hoping that the rules above will process first, but that did not work.
What can I do to make this work as I need it?
First of all if there are mod_alias like redirect and mod_rewrite like RewriteRule mod_rewrite rules get executed before mod_alias rules.
There are many senarios to sove this issue like excluding all other redirect rules from the main rewriterule but it think if the other rules redirect have same target to only ommit last part of URI you could make rules like the folwoing :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([^\/]+)/([^\/]+)/([^\/]+)/([^\/]+)\.(html)$ /$1/$2/$3 [R=301,L]
RewriteRule ^(.*)\.(html)$ /$1 [R=301,L]
</IfModule>
Rules above will check if URI is coming in this form /something/something/something/something.html like /about-us/photo-gallery/17577-album-mca-corona-clay-tile-roof-princeton-texas/131173-58e29f485064eprinceton-texas-roofing-services-1.html then redirect it into /something/something/something and if not coming in this way , the second rule will be applied.
Note: clear browser cache then test

htaccess simple Redirect doesn't work with trailing slash

I find a lot of answers to this question (and I have read dozens of them), but they are all about more advanced stuff with patterns and such stuff.
I just need a very simple and basic redirect for static urls.
If I add a trailing slash to the url, the redirect doesn't work and I just can't figure out why.
Example:
RewriteEngine On
Redirect 301 /content https://www.example.com/site/content.html
Redirect 301 /content/ https://www.example.com/site/content.html
https://example.com/content does work, https://example.com/content/ redirects to https://example.com/site/
What is the problem here?
Don't mix mid_rewrite rules with Redirect (mod_alias). Use this rule as very first rule in your root .htaccess:
RewriteEngine On
RewriteRule ^content/?$ https://www.example.com/site/content.html [L,NC,R=302]

Redirect all urls which contain certain parameters to another url which follows a certain pattern

Unfortunately I didn't get it solved by myself and need to ask for help. I want to redirect all urls which follow a certain pattern (in this case it contains "reviews/category"). These URLs supposed to be redirect to another url which is made up the first one:
http://[product-url]/reviews/category/[category-url]
supposed to be redirect to
http://[product-url].html
Furthermore it shouldn't matter if you call the url with or without www.
Example:
http://example.com/ford-blues/reviews/category/cars supposed to be redirect to http://example.com/ford-blues.html
Any help would be much appreciated.
Following code isn't working
RewriteEngine On
RewriteRule ^reviews/category/?$ $1\.html [R=301,L]
Try:
RedirectMatch 301 ^(.*)/reviews/category/ /$1.html
in the htaccess file in your document root.
Or using mod_rewrite:
RewriteEngine On
RewriteRule ^(.*)/reviews/category/ /$1.html [L,R=301]

Redirect only one URL with parameter

I need to redirect URL, for example:
www.mydomain.com/category/sub-category/product?page=shop.ask&flypage=flypage.tpl&product_id=181&category_id=34
To:
www.mydomain.com/category/sub-category/good-product.html
I have a multiple URLs with parameters that need to be redirected to only one or couple of URLs, can you help me, I used Google for hours.
I was try this code at .htaccess:
redirect 301 /category/sub-category/product?page=shop.ask&flypage=flypage.tpl&product_id=181&category_id=34 www.mydomain.com/category/sub-category/good-product.html
But it doesn't work.
You can't match against the query string in a Redirect directive. You'll need to use mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=shop.ask&flypage=flypage.tpl&product_id=181&category_id=34$
RewriteRule ^category/sub-category/product$ /category/sub-category/good-product.html? [L,R=301]

How to redirect these old urls to new ones (subfolder) via htaccess

I am a newbie and would like to redirect all my old urls to new ones (moved as subfolder) via htaccess. Also, with 301 permanent redirect.
Old url pattern looks like:
http://www.mydomain.com/de/myoldpage.html
New url pattern should be:
http://www.mydomain.com/shop/de/myoldpage.html
I am using the same domain and same page urls but only my shop is moved to a subfolder "shop".
How can i write a redirect rule to redirect all the urls in this pattern.
Thanks in advance.
Try this code in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^(de/.*)$ /shop/$1 [L,R=301,NC]
RewriteEngine On
RewriteRule ^de/(.*)$ shop/de/$1 [L,R]

Resources