301 Redirect with variables using htaccess - .htaccess

I upgrade my company's website to a new one while changing page names and variable names. So I want to redirect this dynamic page
http://www.example.com/companies.php?supplier=test1&manufacturer=Test2
to this
http://www.example.com/supplier/test2
NOTE: I need to lowercase the test2 variable
Edit: I have written this code. The page redirects to new page name but variables don't
RewriteCond %{QUERY_STRING} supplier=([^/]+)&manufacturer=([^/]+)
RewriteRule ^/?companies\.php$ supplier/$2

Related

How to redirect (htaccess) any one file on any subdomain to a new single subdomain?

So I have a file
www.example.com/for/ldt.php?do=test
But this file can be found on www1.example.com, www2.example.com and so on
I want to redirect all users no matter what www subdomain the person is coming from to a single subdomain of cdn.example.com
The kicker here is I dont want to redirect for all files or requests, only if they are trying to access ldt.php which is a dynamic script so it can have a bunch of arguments too
Finally, when the change is made with htaccess, is it possible for the url to physically change in the address bar too?
Can all this be done?
You can do it using a rewriteRule that redirects the file based on HTTP_HOST headers or only if the requested host is not cdn.example.com .
RewriteEngine on
RewriteCond %{HTTP_HOST} !^cdn\.example\.com$ [NC]
RewriteRule ^for/ldt\.php$ http://cdn.example.com/for/ldt.php [L,R]
This will automatically append the querystring ?do=test to the end of the destination URL.

Redirect to another domain except for home page

I am trying to redirect my old website to a new domain with a few exceptions. These are the current rules that are in my .htaccess file and they are working great:
RewriteEngine on
# handle both specific URL redirects
RewriteRule ^/?product-(?:tag|categories)/([\w-]+) https://www.newdomain.com/parts?category=$1 [R=301,L,NC,QSA]
# redirect everything else
RewriteRule ^(.*)$ https://www.newdoamin.com/$1 [R=301,L,NE]
Now I want to add another rule. I want every page to be redirected to the new domain beside the home page on the old site. With that being said, I still want these exsiting rules to work if it is not the home page.
How can I add another redirect rule to my current htaccess that will not redirect when someone access the home page on the old domain and still apply my current rules?
I was trying to use this answer but I couldn't get my existing rules to work with it.
This should work:
RewriteEngine on
# handle both specific URL redirects
RewriteRule ^/?product-(?:tag|categories)/([\w-]+) https://www.newdomain.com/parts?category=$1 [R=301,L,NC,QSA]
# redirect everything else
RewriteRule ^/?(.+)$ https://www.newdoamin.com/$1 [R=301,L,NE]
.+ matches every URI except the home page. Make sure to clear your browser cache before testing.

htaccess redirect from page with specific query string

I'm new to the rewrite rules in htaccess and need some help with the following:
Page I want to redirect from:
http://example.com/webinar/?confirmed
Page I want to redirect to when a user tries to access the above URL:
http://example.com/webinar-thanks
What makes this tricky is that I will still need to access the original URL with different query strings such as:
webinar/?stats
webinar/?console
And so on. I tried to do a 301 redirect but that redirects any attempt to access the /webinar page, regardless or the query string at the end, so I guess I need some rewrite syntax?
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^confirmed$
RewriteRule ^webinar/?$ /webinar-thanks [L,R]

301 redirect that carries a variable to new URL (using htaccess)

I need to redirect the pages of an old website to a new website using htaccess. Some of the old site's URLs have a parameter with a variable. That variable ideally would be carried over to the new URL. So for example,
Old: http://old-website.com/index4.php?show=1590
New: http://new-website.com/performance/1590
Notice the variable "1590" is carried to the new URLs. I'm still learning regex and htaccess but can get this working right. Here is where I left off...
RewriteEngine On
RewriteCond %{QUERY_STRING} page=/index4.php?show=([0-9]+)
RewriteRule ^/?(index4.php)?$ http://showfile.fm/performance/%1? [L,R=301]
I also need to get the homepage and other pages without variables to redirect to specific pages. I'm not sure how you would stack those other rules - if there is a necessary order.

htaccess redirect .asp page but allow same page with variables to serve

using htaccess (isapi) I need to redirect .asp page but leave .asp page WITH variables untouched. This sounds simple and so I thought it was but my efforts have been fruitless.
RewriteRule ^CodePage.asp$ ^NewCodePage.asp? [R=301,NC,L]
I want to match EXPLICITLY that exact URL to the new Root Landing page but leave all dynamic pages served from CodePage.asp untouched.
e.g. http://www.domain.com/CodePage.asp?prodid=666
I thought the '$' sign would achieve this but the rule is matching ALL full path dynamic pages as well and pushing them to the NewCodePage.asp page. Note, I do not want to pass the variables to the new page, just the root landing page.
Thank you!
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^CodePage\.asp$ NewCodePage.asp [R=301,NC]

Resources