如何重定向到具有多个域的主页?

How can I redirect to my homepage with multiple domains?

我们公司使用几个 url 来获取相同的内容。对于这个问题,我将使用:

  1. http://example.com
  2. http://example2.com
  3. http://example3.com

我们一直在收到对本不应该存在的页面的虚假请求(例如 wp-login.php - 我们正在使用 Drupal)。为此,我想将他们重定向到我们的主页。我在我的 .htaccess 文件中尝试了以下内容,但它没有用。

Redirect 301 /wp-login.php /

因为我使用了 3 个不同的 url,所以我不确定该怎么做。

我想要发生的是:

  1. http://example.com/wp-login.php redirects to http://example.com/
  2. http://example2.com/wp-login.php redirects to http://example2.com/
  3. http://example3.com/wp-login.php redirects to http://example3.com/

编辑: 这是我的整个 htaccess 文件:

#Redirect files
Redirect 301 /apple-touch-icon.png /misc/apple-touch-icon.png
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^wp-login.php(.*)? / [R=301]

#AuthName "Restricted Area"
#AuthType Basic
#AuthUserFile /var/www/content/.htpasswd
#AuthGroupFile /dev/null
#require valid-user
#
# Apache/PHP/Drupal settings:
#


# Protect files and directories from prying eyes.
<FilesMatch "\.(engine|inc|info|install|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl|svn-base)$|^(code-style\.pl|Entries.*|Repository|Root|Tag|Template|all-wcprops|entries|format)$">
  Order allow,deny
</FilesMatch>

# Don't show directory listings for URLs which map to a directory.
Options -Indexes

# Follow symbolic links in this directory.
Options +FollowSymLinks

#compress certain files
<FilesMatch "\.(js|css|html|htm|php|xml)$">
    SetOutputFilter DEFLATE
</FilesMatch>

# Make Drupal handle any 404 errors.
ErrorDocument 404 /index.php

# Force simple error message for requests for non-existent favicon.ico.
<Files favicon.ico>
  # There is no end quote below, for compatibility with Apache 1.3.
  ErrorDocument 404 "The requested file favicon.ico was not found.
</Files>

# Set the default handler.
DirectoryIndex index.php

# Override PHP settings. More in sites/default/settings.php
# but the following cannot be changed at runtime.

# PHP 4, Apache 1.
<IfModule mod_php4.c>
  php_value magic_quotes_gpc                0
  php_value register_globals                0
  php_value session.auto_start              0
  php_value mbstring.http_input             pass
  php_value mbstring.http_output            pass
  php_value mbstring.encoding_translation   0
</IfModule>

# PHP 4, Apache 2.
<IfModule sapi_apache2.c>
  php_value magic_quotes_gpc                0
  php_value register_globals                0
  php_value session.auto_start              0
  php_value mbstring.http_input             pass
  php_value mbstring.http_output            pass
  php_value mbstring.encoding_translation   0
</IfModule>

# PHP 5, Apache 1 and 2.
<IfModule mod_php5.c>
  php_value magic_quotes_gpc                0
  php_value register_globals                0
  php_value session.auto_start              0
  php_value mbstring.http_input             pass
  php_value mbstring.http_output            pass
  php_value mbstring.encoding_translation   0
  php_value post_max_size                   300M
  php_value memory_limit                    1000M
  php_value upload_max_filesize             300M
  php_value max_execution_time              600000000
  php_value session.hash_bits_per_character             4
  php_value session.save_path    "/var/lib/php5"
  #php_admin_flag engine on
  php_flag xcache.cacher 1
  php_flag xcache.optimizer 1
  #php_flag eaccelerator.enable 1
  #php_flag eaccelerator.optimizer 1
</IfModule>

# Requires mod_expires to be enabled.
<IfModule mod_expires.c>
  # Enable expirations.
  ExpiresActive on
  ExpiresByType image/jpeg A604800
  ExpiresByType image/gif A604800
  ExpiresByType image/png A604800
  ExpiresByType application/x-shockwave-flash A604800
  ExpiresByType audio/mpeg A604800

  # Cache all files for 0 weeks after access (A).
  # ExpiresDefault "access plus 3 days"

  #Caching Removed
  ExpiresDefault A1
  Header unset Cache-Control
  Header append Cache-Control: "no-cache,must-revalidate"

  # Do not cache dynamically generated pages.
  #ExpiresByType text/html A1
</IfModule>

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on

  # If your site can be accessed both with and without the 'www.' prefix, you
  # can use one of the following settings to redirect users to your preferred
  # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
  #
  # To redirect all users to access the site WITH the 'www.' prefix,
  # (http://example.com/... will be redirected to http://www.example.com/...)
  # adapt and uncomment the following:
        RewriteCond %{THE_REQUEST} ^.*/index.html
        RewriteRule ^(.*)index.html$ http://www.example.com/ [R=301,L]

    RewriteCond %{HTTP_HOST} ^example\.com [NC]
    RewriteRule ^(.*) http://www.example.com/ [R=301,L]
  #
  # To redirect all users to access the site WITHOUT the 'www.' prefix,
  # (http://www.example.com/... will be redirected to http://example.com/...)
  # uncomment and adapt the following:
  # RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
  # RewriteRule ^(.*)$ http://example.com/ [L,R=301]

  # Modify the RewriteBase if you are using Drupal in a subdirectory or in a
  # VirtualDocumentRoot and the rewrite rules are not working properly.
  # For example if your site is at http://example.com/drupal uncomment and
  # modify the following line:
  # RewriteBase /drupal
  #
  # If your site is running in a VirtualDocumentRoot at http://example.com/,
  # uncomment the following line:
  RewriteBase /

  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q= [L,QSA]
</IfModule>


# $Id: .htaccess,v 1.90.2.3 2008/12/10 20:04:08 goba Exp $

编辑: 更新了 htaccess 文件

#Redirect files
Redirect 301 /apple-touch-icon.png /misc/apple-touch-icon.png


#AuthName "Restricted Area"
#AuthType Basic
#AuthUserFile /var/www/content/.htpasswd
#AuthGroupFile /dev/null
#require valid-user
#
# Apache/PHP/Drupal settings:
#


# Protect files and directories from prying eyes.
<FilesMatch "\.(engine|inc|info|install|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl|svn-base)$|^(code-style\.pl|Entries.*|Repository|Root|Tag|Template|all-wcprops|entries|format)$">
  Order allow,deny
</FilesMatch>

# Don't show directory listings for URLs which map to a directory.
Options -Indexes

# Follow symbolic links in this directory.
Options +FollowSymLinks

#compress certain files
<FilesMatch "\.(js|css|html|htm|php|xml)$">
    SetOutputFilter DEFLATE
</FilesMatch>

# Make Drupal handle any 404 errors.
ErrorDocument 404 /index.php

# Force simple error message for requests for non-existent favicon.ico.
<Files favicon.ico>
  # There is no end quote below, for compatibility with Apache 1.3.
  ErrorDocument 404 "The requested file favicon.ico was not found.
</Files>

# Set the default handler.
DirectoryIndex index.php

# Override PHP settings. More in sites/default/settings.php
# but the following cannot be changed at runtime.

# PHP 4, Apache 1.
<IfModule mod_php4.c>
  php_value magic_quotes_gpc                0
  php_value register_globals                0
  php_value session.auto_start              0
  php_value mbstring.http_input             pass
  php_value mbstring.http_output            pass
  php_value mbstring.encoding_translation   0
</IfModule>

# PHP 4, Apache 2.
<IfModule sapi_apache2.c>
  php_value magic_quotes_gpc                0
  php_value register_globals                0
  php_value session.auto_start              0
  php_value mbstring.http_input             pass
  php_value mbstring.http_output            pass
  php_value mbstring.encoding_translation   0
</IfModule>

# PHP 5, Apache 1 and 2.
<IfModule mod_php5.c>
  php_value magic_quotes_gpc                0
  php_value register_globals                0
  php_value session.auto_start              0
  php_value mbstring.http_input             pass
  php_value mbstring.http_output            pass
  php_value mbstring.encoding_translation   0
  php_value post_max_size                   300M
  php_value memory_limit                    1000M
  php_value upload_max_filesize             300M
  php_value max_execution_time              600000000
  php_value session.hash_bits_per_character             4
  php_value session.save_path    "/var/lib/php5"
  #php_admin_flag engine on
  php_flag xcache.cacher 1
  php_flag xcache.optimizer 1
  #php_flag eaccelerator.enable 1
  #php_flag eaccelerator.optimizer 1
</IfModule>

# Requires mod_expires to be enabled.
<IfModule mod_expires.c>
  # Enable expirations.
  ExpiresActive on
  ExpiresByType image/jpeg A604800
  ExpiresByType image/gif A604800
  ExpiresByType image/png A604800
  ExpiresByType application/x-shockwave-flash A604800
  ExpiresByType audio/mpeg A604800

  # Cache all files for 0 weeks after access (A).
  # ExpiresDefault "access plus 3 days"

  #Caching Removed
  ExpiresDefault A1
  Header unset Cache-Control
  Header append Cache-Control: "no-cache,must-revalidate"

  # Do not cache dynamically generated pages.
  #ExpiresByType text/html A1
</IfModule>

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^wp-login.php(.*)? / [R=301]

  # If your site can be accessed both with and without the 'www.' prefix, you
  # can use one of the following settings to redirect users to your preferred
  # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
  #
  # To redirect all users to access the site WITH the 'www.' prefix,
  # (http://example.com/... will be redirected to http://www.example.com/...)
  # adapt and uncomment the following:
        RewriteCond %{THE_REQUEST} ^.*/index.html
        RewriteRule ^(.*)index.html$ http://www.example.com/ [R=301,L]

    RewriteCond %{HTTP_HOST} ^example\.com [NC]
    RewriteRule ^(.*) http://www.example.com/ [R=301,L]
  #
  # To redirect all users to access the site WITHOUT the 'www.' prefix,
  # (http://www.example.com/... will be redirected to http://example.com/...)
  # uncomment and adapt the following:
  # RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
  # RewriteRule ^(.*)$ http://example.com/ [L,R=301]

  # Modify the RewriteBase if you are using Drupal in a subdirectory or in a
  # VirtualDocumentRoot and the rewrite rules are not working properly.
  # For example if your site is at http://example.com/drupal uncomment and
  # modify the following line:
  # RewriteBase /drupal
  #
  # If your site is running in a VirtualDocumentRoot at http://example.com/,
  # uncomment the following line:
  RewriteBase /

  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q= [L,QSA]
</IfModule>


# $Id: .htaccess,v 1.90.2.3 2008/12/10 20:04:08 goba Exp $

我建议您采用以下更灵活的解决方案:

# Various rewrite rules.
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^wp-login.php(.*)? / [R=301]

    ... other rules ...

</IfModule>

我们正在匹配以 wp-login.php 开头的任何内容,并重定向到 root/base。

将此规则放在 RewriteEngine On 行的正下方:

RewriteRule (^|/)wp-login\.php(/|$) /? [L,NC,R=302]