htaccess rewrite rule for a part of URL - .htaccess

I try to change over htaccess from
http://www.mydomain.com/pictures/generated/product/1/280_280_75/nopic.jpg
to
http://www.mydomain.com/pictures/generated/product/1/280_280_75/nopic1.jpg
Here is my rules
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule nopic\.jpg$ nopic1\.jpg
It works in some way;) The whole URL will be changed just to "nopic1.jpg".
The question is: How can I change only the last part of URL?

This should work:
RewriteRule ^(.*)nopic\.jpg$ $1nopic1\.jpg

Related

How do I do a redirect for a URL with a question mark and equals sign?

I have the URL of:
https://test.co.uk/blog/?p=308
And I want to redirect it to:
https://test.co.uk/category/article
How do I do this in an .htaccess file? I tried a regular 301 redirect in the .htaccess file, but it doesn't work because of the ? and = symbols.
I got the answer here... https://www.301-redirect.online/htaccess-rewrite-generator
It was...
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p\=308$
RewriteRule ^blog/$ https://test.co.uk/category/article/? [R=301,L]

301 Redirect with mod_rewrite

I have some SEO to do on my site.
I'd like that the page 'red-bags' point to the search page 'search.php?keyword=red bags'
In addittion I'd like that the old search page makes a 301 redir to the new red-bags.
RewriteBase /
RewriteRule ^red-bags /search\.php/keyword=red\sbags [NC,L]
RewriteCond %{QUERY_STRING} keyword=red\sbags
RewriteRule ^search.php http://www.mysite.com/red-bags [R=301,L]
The second rules not work. Please help.
Try changing the second rule to this:
RewriteCond %{QUERY_STRING} keyword=red%20bags
RewriteRule ^search.php http://www.mysite.com/red-bags? [R=301,L]
In the first line the space is changed to the urlencoded %20
The question mark at the end of red-bags strips off the query string, otherwise it would have automatically been appended to the new url.

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

htaccess redirect with parameters not working

I'm struggling with an Apache rewriterule. I need to do the following:
Redirect permanently:
http://domain.com/folder/viewer/data/settings.xml?prevent_cache=4760
to
http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml?prevent_cache=4760
I've got the code below, it works without the url parameters but I can't seem to get it to work with parameters. Am i missing something?
RewriteCond %{QUERY_STRING} ^prevent_cache=([0-9]*)$
RewriteRule ^/folder/viewer/data/settings.xml$ http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml [R=301,L]
Cheers
Shaun
The only error I can see, is the leading slash / in the RewriteRule pattern. This should be
RewriteCond %{QUERY_STRING} ^prevent_cache=[0-9]*$
RewriteRule ^folder/viewer/data/settings.xml$ /siteid/includes/themes/siteid/swfs/viewer/data/settings.xml [R,L]
You don't need to append the query string to the substitution URL, because this is done autmoatically.
When everything works as you expect, you can change R to R=301. Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.
I can. Here are the rewrite condition and rule that you're looking for:
# once per htaccess file
RewriteEngine on
RewriteCond %{QUERY_STRING} prevent_cache=([0-9]*)
RewriteRule ^folder/viewer/data/settings.xml http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml?prevent_cache=%1 [R=301,L]
But please considered this answer about the [R=301] flag: https://stackoverflow.com/a/15999177/2007055

htaccess redirects from URLs on one domain to the root of another domain

Currently what is happening is people are accessing old URLs from google like icpaweb.com/site/pages/about-us/ and being sent to their corresponding urls on icpaweb.org : icpaweb.org/site/pages/about-us.
What I want is to send people from: icpaweb.com/site/pages/about-us to icpaweb.org/ without any of the succeeding url segments.
How do I do this?
If you have to use an .htaccess file, you can use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} icpaweb.com$ [NC]
RewriteRule .* http://icpaweb.org/ [R=301,L]
That will 301 redirect all requests for icpaweb.com to the index root of icpaweb.org. If you don't want 301, it can just be R.
You'll need to replace or turn off whatever mechanism is doing your redirecting now, they may not be compatible.
Use an url rewrite rule.
2 steps:
Write a RewriteCond so that the following rewrite rule only apply for url with host being icpaweb.com like RewriteCond %{HTTP_HOST} icpaweb.com$ [NC] The [NC] is for case insensitive match
Write a rewrite rule that convert all input to what you want like RewriteRule ^.*$ http://icpaweb.org/ [L]The [L] is to stop the rewriting to this rule if rule executed.

Resources