Url rewrite not working for first segment of url - apache

Here is the rule I use in my .htaccess :
RewriteCond %{HTTP_HOST} ^domaina.com
RewriteRule ^(.*) http://domainb.com/$1 [P]
For some reason this works perfectly for everything such as http://domaina.com/something-here but fails miserably only for requests to http://domaina.com
Can anyone shed some light as to why that is ?
Thank you.
EDIT
Problem solved, I had forgotten the http:// part! However, this brought about a new problem: http://domaina.com/john doesn't work, but http://domaina.com/john/doe does...

Try adding L (last) flag:
RewriteCond %{HTTP_HOST} ^domaina\.com$ [NC]
RewriteRule ^(.*)$ http://domainb.com/$1 [P,L]

As it turns out, it was a matter of placement :
Other rewrite rules I had were on top of the new ones and therefore overriding what I was trying to create.

Related

Modify query string value using htacces

I want to redirect
https://www.example.com/signup?plan=basic to https://www.example.com/signup?plan=basic-monthly and https://www.example.com/signup?plan=pro to https://www.example.com/signup?plan=pro-monthly .
How can I achieve this using htaccess ?
There are many questions related to this here. But, couldn't find an answer for this specific scenario.
This is the code I tried and failed:
RewriteCond %{QUERY_STRING} (^|&)plan=pro(&|$)
RewriteRule ^signup /$0?plan=pro-monthly [R=301,L]
Also, while trying the same with "basic" instead of "pro", the word "basic" i shown in red color as if it is a keyword.
Could you please try following, written with shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^(plan=(?:basic|pro))$ [NC]
RewriteRule ^(signup)/?$ $1?%1-monthly [L]
2nd solution: Or you could try following too. Make sure you either put 1st solution rules OR this one at a time.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(signup)\?(plan=(?:basic|pro))\s [NC]
RewriteRule ^ %1?%2-monthly [L]

Rewrite one directory to another .htaccess

I want to automatically redirect http requests to news/images to ../images.
Is that possible with .htaccess?
Thing is: request to www.site.tld/news/images ... should go to www.site.tld/images ...
I have tried:
RewriteEngine On
...
...
RewriteRule (.*)news/images(.*) ../images [R=301,L]
not working.
I have ensured that apache have mod_rewrite.c enabled.
To redirect all requests for /news/images/ to /images/, capture the part after images and use it in the RewriteRule
RewriteRule ^news/images(.*)$ /images$1 [R,L]
When it works as it should, you may replace R with R=301. Never test with R=301.
You can use:
RewriteRule ^www\.site\.tld/news/images$ /www.site.tld/images?&%{QUERY_STRING}
or you can also use:
RewriteCond %{HTTP_HOST} ^www.site.tld/news/images$ [NC]
RewriteRule ^(.*)$ http://www.site.tld/images/$1 [R=301,L]
But as #arkascha said, please do some research first, there are MANY answers to this sort of problem! :) Either way, I hope this helps.

htaccess RewriteRule for folder

I have a bunch of rewrite rules I would like to implement. I would like to redirect anything that has /blog/tag/... to my root url.
For example, all of these:
blog/tag/button-sets/
blog/tag/icons/
blog/tag/order-now/
blog/tag/body-attributes/
Would simply route to: www.url.com
I can do it on a case-by-case basis like below, but would like to redirect a bunch with 1 rule. Any help would be greatly appreciated
RewriteCond %{HTTP_HOST} ^url\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.url\.com$
RewriteRule ^blog\/tag\/button\-sets\/?(.*)$ "http\:\/\/www\.url\.com\/$1" [R=301,L]
Why dont you just remove the button-sets part of your rule like so
RewriteRule ^blog\/tag\/?(.*)$ "http\:\/\/www\.url\.com\/" [R=301,L]
Havent tested it but should be OK?
In stead make it such that it captures everything after the second / after blog/tag/(ANYTHING)/(CAPTURE) which will redirect to www.url.com/CAPTURE
RewriteRule ^blog\/tag\/.*\/?(.*)$ "http\:\/\/www\.url\.com\/$1" [R=301,L]

Mod Rewrite, Unexpected Results

We are trying to redirect everything from one domain to another with the following
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .? http://www.example2.com%{REQUEST_URI} [R=301,L]
When we visit http://www.example.com/v2sc
We are being redirected to http://www.example2.comv2sc
We would like to be redirected to http://www.example2.com/v2sc considering www.example2.comv2sc is not a valid hostname
Any ideas on how we can accomplish this?
Thank you!
It seems like you're using a .htaccess file for this. In that context the leading slash is not present in %{REQUEST_URI} so it's up to you to put it back in.
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example2.com/%{REQUEST_URI} [R=301]
Please also note that solutions like this should be used only if you cannot edit the main server configuration file. Doing so would allow you to use a cleaner combination of vhosts and Redirect directives that would run much more quickly.

Why is my simple rewrite code causing a redirect loop?

Would greatly appreciate it if someone could help me make this work.
I'm trying to make domain.com/painting.php?name=hello redirect to domain.com/page/hello while keeping my rewrite :
RewriteCond %{QUERY_STRING} name=([^&]+)
RewriteRule ^painting\.php$ /page/%1? [R=301,L] #redirects to page
RewriteRule ^page/([^/\.]+)/?$ painting.php?name=$1 [L] #rewrites painting
I would like to keep only the "pretty url". Please help.
Found answer here: simple 301 redirect with variable not working, why?
I'm assuming what you actually want to do is be able to accept the URL domain.com/page/hello and rewrite it (invisibly) to domain.com/painting.php?name=hello. If so, try this
RewriteRule ^page/([^/.])+/?$ painting.php?name=$1 [QSA,L]