htaccess redirecting one query string to another - apache

I'd like to redirect http://domain.com/videos/?src=category1&video=video1
To http://domain.com/videos/watch?v=1
The obvious, just using a Redirect 301 /old /new doesn't work here. Here's my starting point and I can't seem to get this to redirect properly. This .htaccess file lives in the videos directory.
RewriteCond %{QUERY_STRING} ^src=category1&video=video1$ [NC]
RewriteRule /videos/watch\?v=1 [L,R=301]

Try:
RewriteCond %{QUERY_STRING} ^src=category1&video=video1$ [NC]
RewriteRule ^$ /videos/watch?v=1 [L,R=301]
You were missing the rewrite rule's regex pattern.

Related

301 working on some URLs and not on others in htaccess

Hi I am having an issue with .htaccess on 301 redirects.
Everything looks fine, on 90% of the urls but not for any of the individual blog post redirects.
here is the code where they do not redirect:
RewriteRule ^blog-article.php?id=38 blog-post/xxxx-xxxxxx-xx-x-xxxxxxx/9/ [L,NC,R=301]
RewriteRule ^blog-article.php?id=34 blog-post/xxxxxxx/4/ [L,NC,R=301]
But the all of the other 301 rewrites work, and they look like this:
RewriteRule ^old-url-portfolio/(.*)$ page/new-url-portfolio/$1 [L,NC,R=301]
RewriteRule ^Portfolio/Detail/(.*)$ portfolio/$1 [L,NC,R=301]
RewriteRule ^old-url/7/ page/new-url/ [L,NC,R=301]
RewriteRule ^contact-us-old/ page/contact-us-new/ [L,NC,R=301]
RewriteRule ^blog.php page/blog/ [L,NC,R=301]
I can not find why they are not working, its not because the ones that are not working are individual files because the blog.php file is being sent to the new page.
Any ideas would be appreciated :)
You can not match against querystring in RewriteRule's pattern. To redirect blog-article.php?id=123 to diffrent location , you need to match against %{QUERY_STRING} using RewriteCond directive.
RewriteCond %{QUERY_STRING} ^id=123$
RewriteRule ^blog-article.php$ /location/? [L,R]
Note :The traling ? at the end of the destination path is important as it discards the old querystring.
Found a work around, it seams the issue is the ?id=number so I simply did this:
RewriteCond %{QUERY_STRING} ^id=38$
RewriteRule ^blog-article\.php$ http://www.xxxxxxx.co.uk/blog-post/xxxx-xxxxxx-xxxx/9/ [R=301,L]

apache Rewrite rule appends /html directory for no reason

I have a domain foo.tech.
I want to use a new domain footech.io instead.
The redirect also has to make sure all the URLs work.
E.g foo.tech/bar goes to footech.io/bar
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^(.*) http://footech.io/$1 [R=301,L]
For some reason, it decides to add /html at the end of my domain.
So now if I visit foo.tech it will redirect to footech.io/html
If I visit foo.tech/bar it will redirect to footech.io/html/bar
Please help.
Update:
I think the /html comes from the $1
I've tried to make the rewrite rule as follows:
RewriteRule ^(.*) http://footech.io/$1/$1 [R=301,L]
going to foo.tech leads to footech.io/html//html/
going to foo.tech/bar leads to footech.io/html/bar/html/bar
final update:
I made it work now using this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]
This seems to fix it
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]

.htaccess redirect subdomain dynamicly without changing url

I need to have a .htaccess redirect.
If I go to example.bla.org
I want it to redirect to bla.org/example
EXCEPT: It must keep the subdomain url can needs to be dynamic.
Ex: example.bla.org/apple -> bla.org/example/apple
I have tried almost every method like this one:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^examples.website.org [NC]
RewriteRule ^/(.*)$ /examples/$1 [L]
Please help, thanks!
You're pretty close, try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.website.org [NC]
RewriteRule ^(.*)$ http://website.org/example/$1 [L]
If you want to do a 301 redirect for SEO etc, add R=301 to the rewrite :
RewriteRule ^(.*)$ http://website.org/example/$1 [R=301,L]

RewriteRule showing inconsistence behavior

I have the following rule in my htaccess file (this is the full htaccess file):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?!www\.).+$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST} [L,R=301]
Using the htaccess tester (http://htaccess.madewithlove.be/) it shows the non-www URL is correctly redirected to a www url (test with mydomain.com/ and mydomain.com/subdirectory/ (goes to www.mydomain.com/subdirectory/).
Now, when I put this htaccess file on my site, it will redirect mydomain.com/subdirectory/ to www.mydomain.com instead of to www.mydomain.com/subdirectory/
Why does it show this inconsistent behavior?
You're not using capture value $1 in target:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
So, I looked up what the HTTP_HOST value actually is. It looks like it doesn't contain any further than the domain extension (i.e. domain.com but not anything that's behind that) so it will redirect to domain.com)
I modified the htaccess to the following, which is working:
RewriteCond %{HTTP_HOST} ^(?!www\.).+$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If anyone still has any suggestions how to improve the regex, I'm open for improvements

Redirect Subdomain to new domain

Hi guys trying to get a 301 redirect working and having trouble. I need to redirect sub.domain1.com to www.domain2.com and make sure that any file names or parameters get sent over with it.
This is what I was trying:
RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
I also tried this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^sub\.domain1\.com$ /www.domain2.com? [R=301,NE,NC,L]
Where am I messing up?
You missed the subdomain part and proper escaping.
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
Further explain can be found in this question.
Rule of thumb for rewriterules: from the most complex to the less complex.
And don't forget the QSA directive (QSA = Query String Append = "make sure that any file names or parameters get sent over with it")
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [QSA,R=301,L]
Tell me if it works.