.htaccess. How to add slash to home page? - .htaccess

I'm trying to get rid of all possible dublicates of the main page. And right now I have 2 urls that respond with 200 status code. They are https://host.com and https://host.com/. Is there a way to redirect from https://host.com to https://host.com/?
I already tried something like this, but it didn't work
RewriteCond %{QUERY_STRING} ^$ [NC]
RewriteRule ^((.*/)?[^/\.]+)$ /$1/ [R,L]

Related

redirect with htaccess and parameters

I moved from a wordpress to a typo3 site. On my old sites, the links looked like this:
http://www.example.org/?page_id=44
now I want to redirect it to:
http://www.example.org/contact
Usually the redirects-rules are no problem, but this time I don't get it why it is not working:
I tried this:
Redirect 301 /?page_id=44 http://www.example.org/contact
as well as this:
RewriteRule http://www.example.org/\?page_id=44 http://www.example.org/contact [R=301,L]
and this here:
RewriteCond %{REQUEST_URI} ^example.org/\?page_id=44
RewriteRule ^(.*)$ http://www.example.org/contact [R=301,L]
Tried it in several browsers and incognito-mode, but it still remains wrong, it still adds the parameter instead of redirecting to the certain page.
I guess it is somehow doable with %{query_STRING} ?
Is it due to the param-thingy?
Any ideas how to solve this problem?
Okay, got it. It had something to do with the query string:
Final result:
RewriteCond %{QUERY_STRING} ^page_id=14$ [NC]
RewriteRule ^(index\.php){0,1}$ /kontakt/? [L,R=301,NC]
You can use:
RewriteCond %{THE_REQUEST} /\?page_id=44\s [NC]
RewriteRule ^/?$ /contact? [R=301,L]

How to stop 'www' htaccess redirect adding on a second 'www'?

Currently here's my htaccess code:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{query_string} (^|&)p=contact$
RewriteRule (.*) /contact/
RewriteCond %{query_string} ^body=
RewriteRule (.*) $1? [R=301,L]
So what the code does is during the HTML query in URL, any queries that has "p=contact" will be redirected to the contact page. While any query that has the word 'body' will be removed.
Now the code works brilliantly if I visit my site (e.g: example.com).
However, if I try to type www.example.com, it will load as www.www.example.com. It has double wwws. Is there a fix for this?
Okay, I manage to solve it.
After hours of meddling with it, I realize if I change %{HTTP_HOST}% to my domain name it kinda makes it work perfectly.
Strange.

301 redirecting pagination with get variables

I'm trying to set 301 redirects on pages that have 'page=1' in the URL to stop duplicate content issues.
e.g.
http://www.domain.com/reviews/?page=1
to
http://www.domain.com/reviews/
I've tried all of the variations I can find and can't seem to get anything to work.
RewriteRule ^reviews(/)?page=1$ http://www.domain.com/reviews/ [L,R=301]
RewriteRule ^(.*)/?page=1 http://www.domain.com/reviews/ [R=301,L]
RewriteCond %{QUERY_STRING} page=1
RewriteRule ^reviews$ http://www.domain.com/reviews/ [R=301,L,NE]
None of these have worked. I'm not really sure what else to try.
There are multiple different sections of the site that I need to do this for:
reviews
news
videos
accessories
hardware
An overall solution to redirect all ?page=1 URLs to their relevant section would be best.
Use this code to redirect every URI with ?page=1 to one without the query parameter:
RewriteCond %{QUERY_STRING} ^page=1(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
Or else if you want to redirect ONLY /reviews URI then
RewriteCond %{QUERY_STRING} ^page=1(&|$) [NC]
RewriteRule ^(reviews)/?$ /$1? [R=301,L]
the question is already answered, i just would like to mention that your rules are not working because you didn't append a trailing ? to the new url in the rewrite rule

redirect a domain if it isnt a certain file type

I'm in the process of setting up a static cookieless domain, and everything is working great!
But im wanting to know how to redirect if the request isn't to an image/js/css file, to avoid duplicate content from the cookieless domain.
I have this:
RewriteCond %{HTTP_REFERER} ^http://files\.my-static-site\.com/ [NC]
RewriteRule \.(html|php)$ - [F,NC,L]
and it works, but it still loads the home page if you simply put files.my-static-site.com.
If you click on a link on the page your shown a 403. How do i change that to 301 redirect to the main domain instead of showing a 403?
Try changing it to this:
RewriteCond %{HTTP_HOST} ^files\.my-static-site\.com$ [NC]
RewriteRule \.(html|php)$ http://main.domain.com/ [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^files\.my-static-site\.com$ [NC]
RewriteRule ^$ http://main.domain.com/ [R=301,NC,L]

htaccess redirect not working as expected

I know I am missing something simple, but I can't get this redirect to work right. I have a parked domain that I want to redirect to the main domain. I am using:
RewriteCond %{HTTP_HOST} ^parked-domain.com$ [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
The problem is that parked-domain.com/anypage.html redirects to main-domain.com and leaves off the rest of the URL. I need parked-domain.com/anypage.html to redirect to main-domain.com/anypage.html
EDIT
I am sure this is a cache thing with my browser but after I tried the first suggestion then changed it back, now I get this:
parked-domain.com/anypage.html redirects to www.main-domain.com/anypage.html
but
www.parked-domain.com/anypage.html does not redirect at all.
Part of the problem was with the cache, that got part of the redirect working. I ended up using this to get it all forwarding properly:
RewriteCond %{HTTP_HOST} ^parked-domain.com$ [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.parked-domain.com$ [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
The first one should have worked for all cases, but it didn't. Adding the second redirect fixed it.
Try removing the ^ $ around your (.*)
RewriteRule (.*) http://www.main-domain.com/$1 [L,R=301]

Resources