.htaccess redirecting dynamic URL - .htaccess

I am using the following code to attempt to redirect a dynamic URL to a new dynamic URL, under the same domain:
RewriteRule ^products/item/^\d([^/]+) /product/$1/ [R=301,L]
I've tried these too:
RewriteRule ^products/item/[^\d]([^/]+) /product/$1/ [R=301,L]
RewriteRule ^products/item/[0-9]([^/]+) /product/$1/ [R=301,L]
But this was redirecting /products/item/342/ to /product/42/, I tested this on a larger number (e.g. 123456789) and it redirected to /product/23456789/.
It would appear that my rule is not picking up the firsts digit, can anyone help me resolve this?
I've also tried using [\d] instaled of [0-9], but this has the same problem.
Cheers!

Try
RewriteRule ^products/item/(\d[^/]+) /product/$1/ [R=301,L]

RewriteRule ^products/item/([0-9]+) /product/$1/ [R=301,L]

Related

.htaccess multiple redirects with fallback

I'd like to create some kind of redirection script using .htaccess to map short urls (like example.com/1 to other urls). To do so, I've created this:
RedirectPermanent /1 https://www.youtube.com/watch?v=...
RedirectPermanent /2 https://www.youtube.com/watch?v=...
So far, that's working. However, it's missing a fallback. I'd like to add a 404 page that is shown whenever someone tries to navigate a URL that doesn't have any redirect (yet).
I've tried adding this:
...
RewriteCond %{REQUEST_URI} !^/404.html$
RewriteRule .* /404.html [L,R=302]
Obviously, this isn't working, because now ALL calls are redirected to 404.html. I thought about adding the redirects as conditions, but since there might be many redirects that approach seems very bad to me.
What can I do instead?
Thanks for helping out.
I've done this now, but I'm not sure if this is the best solution.
Please comment if you have any advices.
RewriteRule ^/1$ https://www.youtube.com/watch?v=1 [L,R=301]
RewriteRule ^/2$ https://www.youtube.com/watch?v=2 [L,R=301]
RewriteCond %{REQUEST_URI} !^/404.html$
RewriteRule .* /404.html [L,R=302]

.htaccess rewrite domainnames, save subdomain and parameters

i have 2 domains but want use only one.
these some examples of domains...
x://test1.domain1.xx/parameter/1
x://test1.domain1.xx/parameter/2
x://test1.domain1.xx/parameter/3
x://test2.domain1.xx/parameter/1
x://test2.domain1.xx/parameter/2
x://test2.domain1.xx/parameter/3
x://test1.domain2.xx/parameter/1
x://test1.domain2.xx/parameter/2
x://test1.domain2.xx/parameter/3
x://test2.domain2.xx/parameter/1
x://test2.domain2.xx/parameter/2
x://test2.domain2.xx/parameter/3
now i want only use xx.domain2.xx/params... all sub/domains with maindomain "domain1" should be rewritten or redirect. without loose parameters or subdomain.
thanks a lot.
I tried sometimes an i think i got a solution.
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain1.xx [NC]
RewriteRule (.*) http://%2.domain2.xx/$1 [R=301,L]

Complex htaccess subfolder 301-redirection

I have a little problem with htaccess redirection, I have an issue which i can't figure out.
www.old.com/sub should direct to: www.new.com/page/
BUT
all the subpages of www.old.com/sub (www.old.com/sub/1, www.old.com/sub/2), should redirect to www.new.com/category/1, www.new.com/category/2 etc. (insted of page)
But how?
What I tried (i assumed this should work, but it didn't):
RewriteEngine on
RewriteRule ^sub/$ http://www.new.com/page/ [R=301,L]
RewriteRule ^sub(/.*)?$ http://www.new.com/category/$1 [R=301,L]
note; it is part of a large list of redirections, so it should not affect other redirections in this htaccess file.
That work with:
RewriteEngine on
RewriteRule ^sub/?$ http://www.new.com/page/ [R=301,L]
RewriteRule ^sub/(\d+)/?$ http://www.new.com/category/$1 [R=301,L]
You can change (\d+) to (.+) if you do not use numbers only after sub/

htaccess redirect specific url to another domain

I want to redirect specific URL with params to another domain
http://domain.tld/buy/?w=X1234 to http://anotherdomain.tld/product.php?id=X1234
Here my htaccess
RewriteCond %{REQUEST_URI} !^/*buy/(.*)$ [NC]
RewriteRule ^/*buy/?w=([a-zA-Z0-9]+)$ http://anotherdomain.tld/product.php?id=$1 [NC,L,R=301]
but not working. maybe the problem is param ?w=
Thank you
UPDATE: i solve the problem by using Query string, after reading How to REGEX and .htaccess rewrite url
RewriteCond %{QUERY_STRING} ^w=([a-zA-Z0-9]+)$
RewriteRule ^/*buy/$ http://anotherdomain.tld/product.php?id=%1 [NC,L,R=301]
Thank you stackoverflow and folks! i m start understanding regex from here ^_^
That's right, your params won't be picked up, but they can be passed on.
Something like;
RewriteRule ^buy/([a-zA-Z0-9]+)$ http://anotherdomain.tld/product.php?id=$1 [NC,L,R=301]
Where your original url would be; /buy/X1234

Percentage (%) in the joomla blog post url breaking site

Anyone know how to redirect
domain/blog/208-%-top.html
To:
domain/blog/208-top.html
in Joomla?
None of the below works:
RewriteRule ^blog/208-%-top.html$ 208-top.html [R=301,L]
RewriteRule ^blog/208-\%-top.html$ 208-top.html [R=301,L]
RewriteRule ^blog/208-.*-top.html$ 208-top.html [R=301,L]
It works if: url has %20 or %25 and have below in htaccess
RewriteRule ^blog/208-%20-top.html$ 208-top.html [R=301,L]
RewriteRule ^blog/208-%25-top.html$ 208-top.html [R=301,L]
But the problem is blog post has % only. Is there any work around to fix this?
This URL:
domain.com/blog/208-%-top.html
is indeed an invalid URL and would cause HTTP 400 (Bad Request) error in your Apache logs.
As you noted too that any of these 2 URLs are valid:
domain.com/blog/208-%20-top.html
OR
domain.com/blog/208-%25-top.html
And both of them will work well with this Rewrite rule:
RewriteRule ^blog/208-\%-top\.html$ blog/208-top.html [R=301,L,NC]
Your problem may be because apache isn't properly identifying your rewrite target as a URL-path, you'll need to include the full path to fix that.
RewriteRule ^blog/208-\%-top.html$ /blog/208-top.html [R=301,L]

Resources