-1

I want to redirect https://www.example.com to https://example.com. I followed this answer but it doesn't work. By the way, ErrorDocument 404 doesn't work as well.

What do have I have to do? Here is my file:

<IfModule mod_rewrite.c>
RewriteEngine on  
RewriteBase /

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
AddType application/x-httpd-php html 

ErrorDocument 404 https://kocer.org/index.php?p=notfound
</IfModule>
Community
  • 1
  • 1
Koçer
  • 35
  • 1
  • 5
  • Dumping your `mod_rewrite` rules here is not really the way to ask a question - most of those rules are not relevant to the problem at hand. First, search for ["mod_rewrite remove www"](https://duckduckgo.com/?q=mod_rewrite+remove+www), and if that fails, read the docs for `RewriteRule` and let us know where you are getting stuck. If you need more help, please trim your question/code to the code relevant to the problem. – halfer Jan 28 '15 at 14:47
  • Do you have a corresponding close tag for ``? If not, this won't work, and will probably just raise a 500 error. – halfer Jan 28 '15 at 15:14
  • Yes i have. i put here just where the is an error – Koçer Jan 28 '15 at 15:26
  • I have now fixed your question to include the closing tag - where a mistake is pointed out in your questions, please be willing to fix them in future. – halfer Jan 28 '15 at 15:42

1 Answers1

0

You can use this:

<IfModule mod_rewrite.c>
    RewriteEngine on  
    RewriteBase /

    RewriteCond %{HTTP_HOST}#%{HTTPS}s ^www\.([^#]+)#(?:off|on(s)) [NC]
    RewriteRule ^ http%2://%1%{REQUEST_URI} [NE,L,R=301]
</IfModule>
AddType application/x-httpd-php html 
ErrorDocument 404 /index.php?p=notfound

ErrorDocument Directive
Note that when you specify an ErrorDocument that points to a remote URL (ie. anything with a method such as http in front of it), Apache HTTP Server will send a redirect to the client to tell it where to find the document, even if the document ends up being on the same server. This has several implications, the most important being that the client will not receive the original error status code, but instead will receive a redirect status code. This in turn can confuse web robots and other clients which try to determine if a URL is valid using the status code.

Croises
  • 18,570
  • 4
  • 30
  • 47
  • Internal Server Error! – Koçer Jan 28 '15 at 15:28
  • You use only this code in your .htaccess ? – Croises Jan 28 '15 at 15:33
  • No, whole text. https://kocer.org/stackover.htaccess – Koçer Jan 28 '15 at 15:42
  • @Koçer: your response ("Internal server error!") doesn't give Croises anything further to go on. Please look in your server error logs, and supply the error you are getting. – halfer Jan 28 '15 at 15:43
  • No, test just with mine. – Croises Jan 28 '15 at 15:43
  • i did with kocer.org/beta it not works. you can see by yourself – Koçer Jan 28 '15 at 15:48
  • it works here... try to clear your cache or with other browser – Croises Jan 28 '15 at 15:52
  • Did you tried with http**s**://www.kocer.org/beta ??? – Koçer Jan 28 '15 at 16:02
  • No I try without s, to avoid ssl certificat error, but no Internal Server Error! – Croises Jan 28 '15 at 16:04
  • I don't want ssl error. i want to be redirected to without www. That is why i asked the question. Thank you very much – Koçer Jan 28 '15 at 16:07
  • You can't avoid ssl error, because browser negociate ssl before loading page and redirection... – Croises Jan 28 '15 at 16:08
  • So, no way to do ??? – Koçer Jan 28 '15 at 16:10
  • No way to avoid ssl error if they try with www and your certificate is not compatible. Or you can change your SSL certificate. – Croises Jan 28 '15 at 16:14
  • @Croises I changed my ssl certificate. It is not warning for certificate now. Every things are OK bu a little problem. When i am on https my "not found" page works well. But when i put lets say **http**://example.com/notexis it stays on port 80 and return with errors apache but not my "not found" page. Any idée? – Koçer Jan 29 '15 at 17:55
  • It is possible that this is still a problem with the cache, like you did a lot of testing. But otherwise I see no reason, if you use `ErrorDocument` without http. – Croises Jan 29 '15 at 19:20
  • In your error page, all links must be absolute, or from root. Like you use normal index page, do not forget to add in the html head ``: – Croises Jan 29 '15 at 19:35