سؤال

أود إعادة التوجيه www.example.com إلى example.com. رمز HTACCESS التالي يجعل هذا يحدث:

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

ولكن ، هل هناك طريقة للقيام بذلك بطريقة عامة دون ترميز اسم المجال؟

هل كانت مفيدة؟

المحلول

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

مثل مايكل باستثناء هذا واحد يعمل: ص

نصائح أخرى

ولكن إذا كنا بحاجة إلى القيام بذلك من أجل HTTP و HTTPS منفصلة:

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

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

إعادة توجيه غير WWW إلى www (كلاهما: http + https)

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]

إذا كنت ترغب في القيام بذلك في ملف httpd.conf ، فيمكنك القيام بذلك بدون Mod_Rewrite (ويبدو أنه من الأفضل للأداء).

<VirtualHost *>
  ServerName www.example.com
  Redirect 301 / http://example.com/
</VirtualHost>

حصلت على هذا الإجابة هنا: https://serverfault.com/questions/120488/redirect-url-within-apache-virtualhost/120507#120507

فيما يلي القواعد لإعادة توجيه عنوان URL www إلى عدم www:

#########################
# redirect www to no-www
#########################

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

فيما يلي القواعد لإعادة توجيه عنوان URL لا www إلى www:

#########################
# redirect no-www to www
#########################

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

لاحظ أنني استخدمت NE العلم لمنع Apache من الهروب من سلسلة الاستعلام. بدون هذا العلم ، سيقوم Apache بتغيير عنوان URL المطلوب http://www.example.com/?foo%20bar إلى http://www.example.com/?foo%2250bar

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R]

ال RewriteCond يلتقط كل شيء في HTTP_HOST عامل بعد، بعدما ال www. ويحفظه في %1.

ال RewriteRule يلتقط عنوان URL دون قيادة / ويحفظه في $1.

جرب هذا:

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

إذا بدأ المضيف بـ WWW ، فإننا نلصق المضيف بالكامل في بداية عنوان URL ، ثم خلع "www".

يمكن أن يكون هناك الكثير من المعلومات الخاطئة حول إعادة توجيه htaccess. أولاً ، تأكد من تشغيل موقعك على UNIX باستخدام Apache وليس على مضيف Windows إذا كنت تتوقع أن يعمل هذا الرمز.

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]

RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

(تأكد من عدم وجود مساحات خط بين كل سطر من النص ، على الرغم من أنني أضفت مساحة إضافية بين الخطوط بحيث تجعلها على ما يرام في هذه النافذة.)

هذا هو مقتطف واحد من التعليمات البرمجية التي يمكن استخدامها لتوجيه إصدار www من موقعك إلى إصدار http: //. هناك رموز أخرى مماثلة يمكن استخدامها أيضًا.

إكمال معالج www عام ، http/https

لم أر إجابة كاملة. أنا استخدم هذا للتعامل مع شمول www.

  1. نوعي. لا يتطلب معلومات المجال.
  2. القوات www على المجال الأساسي: www.domain.com
  3. يزيل www على النطاقات الفرعية: sub.domain.com
  4. يحافظ على حالة HTTP/HTTPS.
  5. يسمح ملفات تعريف الارتباط الفردية للمجال / المجالات الفرعية

واسمحوا لي أن أعرف كيف يعمل هذا أو إذا تركت ثغرة.

RewriteEngine On
RewriteBase /

# Force WWW. when no subdomain in host
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ [NC]
RewriteCond %{HTTPS}s ^on(s)|off [NC]
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Remove WWW. when subdomain(s) in host     
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|off [NC]
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)(.+\.)(.+\.)(.+)$ [NC]
RewriteRule ^ %1%3%4%5%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/subfolder/$1 [R=301,L]

للمجلد الفرعي

لأولئك الذين يحتاجون إلى الوصول إلى الموقع بأكمله بدون بادئة "www".

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]

من المؤكد أنك تضيف هذا إلى الملف التالي

/site/location/.htaccess 

www to non www with https

RewriteEngine on

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

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

I used the above rule to fwd www to no www and it works fine for the homepage, however on the internal pages they are forwarding to /index.php

I found this other rule in my .htaccess file which is causing this but not sure what to do about it. Any suggestions would be great:

############################################
## always send 404 on missing files in these folders

    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

############################################
## never rewrite for existing files, directories and links

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l

############################################
## rewrite everything else to index.php

    RewriteRule .* index.php [L]
RewriteEngine on
# if host value starts with "www."
RewriteCond %{HTTP_HOST} ^www\.
# redirect the request to "non-www"
RewriteRule ^ http://example.com%{REQUEST_URI} [NE,L,R]

If you want to remove www on both http and https , use the following :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|offs
RewriteRule ^ http%1://example.com%{REQUEST_URI} [NE,L,R]

This redirects Non ssl

to

And SSL

to

on apache 2.4.* you can accomplish this using a Redirect with if directive,

<if "%{HTTP_HOST} =='www.example.com'">
Redirect / http://example.com/
</if>

use: Javascript / jQuery

// similar behavior as an HTTP redirect
window.location.replace("http://www.stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

Or .htaccess:

RewriteEngine On
RewriteBase /
Rewritecond %{HTTP_HOST} ^www\.yoursite\.com$ [NC]
RewriteRule ^(.*)$ https://yoursite.com/$1 [R=301,L]

and The PHP method:

$protocol = (@$_SERVER["HTTPS"]    == "on") ? "https://" : "http://";

if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {
    header('Location: '.$protocol.'www.'.$_SERVER    ['HTTP_HOST'].'/'.$_SERVER['REQUEST_URI']);
    exit;
}

Ajax

$.ajax({
    type: "POST",
    url: reqUrl,
    data: reqBody,
    dataType: "json",
    success: function(data, textStatus) {
        if (data.redirect) {
            // data.redirect contains the string URL to redirect to
            window.location.href = data.redirect;
        }
        else {
            // data.form contains the HTML for the replacement form
            $("#myform").replaceWith(data.form);
        }
    }
});

Using .htaccess to Redirect to www or non-www:

Simply put the following lines of code into your main, root .htaccess file. In both cases, just change out domain.com to your own hostname.

Redirect to www

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^domain\.com [NC]
  RewriteRule ^(.*)$ http://wwwDOTdomainDOtcom/$1 [L,R=301]
</IfModule>

Redirect to non-www

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
  RewriteRule ^(.*)$ http://domainDOTcom/$1 [L,R=301]
</IfModule>

change DOT to => . !!!

The only way I got it to work...

RewriteEngine On
RewriteCond %{HTTP_HOST} ^site\.ro
RewriteRule (.*) http://www.site.ro/$1 [R=301,L]

If you are forcing www. in url or forcing ssl prototcol, then try to use possible variations in htaccess file, such as:

RewriteEngine On
RewriteBase /

### Force WWW ###

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

## Force SSL ###

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

## Block  IP's ###
Order Deny,Allow
Deny from 256.251.0.139
Deny from 199.127.0.259

The selected answer and many other solutions here dropped the the part of the url after /, so basically it always redirected to main domain, at least for me.. So i am adding working sample respecting full path after slash..

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

I am not sure why u want to remove www. But reverse version would be:

# non-www.* -> www.*, if subdomain exist, wont work
RewriteCond %{HTTP_HOST} ^whattimein\.com
RewriteRule ^(.*)$ http://www.whattimein.com/$1 [R=permanent,L]

And advantage of this script is: if u have something like test.whattimein.com or any other (enviroments for developing/testing) it wont redirect U to the original enviroment.

This is updated to work on Apache 2.4:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

The only change vs Michael's is to remove the [NC], which produces the "AH00665" error:

NoCase option for non-regex pattern '-f' is not supported and will be ignored

Hi you can use following rules on your htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top