将一个子目录重定向到另一个子域

时间:2018-10-01 07:07:10

标签: regex apache .htaccess redirect

我正在尝试编写一个Rewrite,它将单个子目录重定向到同一服务器上的另一个子域。在Stackoverflow上有很多类似的问题,但是我不知道如何将它们结合起来。

我已经尝试了一个简单的重定向,但这当然会导致重定向循环:

Redirect 302 /subdirectory http://another-subdomain.test.de/subdirectory

我尝试了很多条件和规则,但无济于事。这是我最初的测试的样子,但是似乎所有部分都没有用:

RewriteCond %{HTTP_HOST} ^subdomain\.test\.de$ [NC]
RewriteRule ^subdirectory(.*)$ http://another-subdomain.test.de/subdirectory/$1 [R=302,L]

要点,我需要将单个目录重定向到同一服务器上的另一个子域。我该怎么办?

#####
#
# Example .htaccess file for TYPO3 CMS - for use with Apache Webserver
#
# This file includes settings for the following configuration options:
#
# - Compression via TYPO3
# - Settings for mod_rewrite (URL-Rewriting)
# - PHP optimisation
# - Miscellaneous
#
# If you want to use it, you have to copy it to the root folder of your TYPO3 installation (if its
# not there already) and rename it to '.htaccess'. To make .htaccess files work, you might need to
# adjust the 'AllowOverride' directive in your Apache configuration file.
#
# IMPORTANT: You may need to change this file depending on your TYPO3 installation!
#
# Lines starting with a # are treated as comment and ignored by the web server.
#
# You should change every occurance of TYPO3root/ to the location where you have your website in.
# For example:
# If you have your website located at http://example.com/
# then your TYPO3root/ is just empty (remove 'TYPO3root/')
# If you have your website located at http://example.com/some/path/
# then your TYPO3root/ is some/path/ (search and replace)
#
# You can also use this configuration in your httpd.conf, but then you have to modify some lines,
# see the comments (search for 'httpd.conf')
#
# Questions about this file go to the matching Install mailing list, see
# http://typo3.org/documentation/mailing-lists/
#
####


### Begin: Compression via TYPO3 ###

# Compressing resource files will save bandwidth and so improve loading speed especially for users
# with slower internet connections. TYPO3 can compress the .js and .css files for you.
# *) Uncomment the following lines and
# *) Set $TYPO3_CONF_VARS['BE']['compressionLevel'] = '9' for the Backend
# *) Set $TYPO3_CONF_VARS['FE']['compressionLevel'] = '9' together with the TypoScript properties
#    config.compressJs and config.compressCss for GZIP compression of Frontend JS and CSS files.

<FilesMatch "\.js\.gzip$">
    AddType "text/javascript" .gzip
</FilesMatch>
<FilesMatch "\.css\.gzip$">
    AddType "text/css" .gzip
</FilesMatch>
AddEncoding gzip .gzip

### End: Compression via TYPO3 ###


### Begin: Browser caching of ressource files ###

# Enable long browser caching for JavaScript and CSS files.

# This affects Frontend and Backend and increases performance.
# You can also add other file extensions (like gif, png, jpg), if you want them to be longer cached, too.

<FilesMatch "\.(js|css)$">
  <IfModule mod_expires.c>
    ExpiresActive on
    ExpiresDefault "access plus 7 days"
  </IfModule>
  FileETag MTime Size
</FilesMatch>

### End: Browser caching of ressource files ###


### Begin: Settings for mod_rewrite ###

# You need rewriting, if you use a URL-Rewriting extension (RealURL, CoolUri).

<IfModule mod_rewrite.c>

# Enable URL rewriting
RewriteEngine On

# Change this path, if your TYPO3 installation is located in a subdirectory of the website root.
RewriteBase /

# Rules to set ApplicationContext based on hostname
#RewriteCond %{HTTP_HOST} ^dev\.example\.com$
#RewriteRule (.*) $1 [E=TYPO3_CONTEXT:Development]
#RewriteCond %{HTTP_HOST} ^staging\.example\.com$
#RewriteRule (.*) $1 [E=TYPO3_CONTEXT:Production/Staging]
#RewriteCond %{HTTP_HOST} ^www\.example\.com$
#RewriteRule (.*) $1 [E=TYPO3_CONTEXT:Production]

# Rule for versioned static files, configured through:
# - $TYPO3_CONF_VARS['BE']['versionNumberInFilename']
# - $TYPO3_CONF_VARS['FE']['versionNumberInFilename']
# IMPORTANT: This rule has to be the very first RewriteCond in order to work!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]

# Basic security checks
# - Restrict access to deleted files in Recycler directories
# - Restrict access to TypoScript files in default templates directories
# - Restrict access to Private extension directories
# For httpd.conf, use these lines instead of the next ones:
# RewriteRule ^/TYPO3root/fileadmin/(.*/)?_recycler_/ - [F]
# RewriteRule ^/TYPO3root/fileadmin/templates/.*(\.txt|\.ts)$ - [F]
# RewriteRule ^/TYPO3root/typo3conf/ext/[^/]+/Resources/Private/ - [F]
RewriteRule ^fileadmin/(.*/)?_recycler_/ - [F]
RewriteRule ^fileadmin/templates/.*(\.txt|\.ts)$ - [F]
RewriteRule ^typo3conf/ext/[^/]+/Resources/Private/ - [F]

# Stop rewrite processing, if we are in the typo3/ directory.
# For httpd.conf, use this line instead of the next one:
# RewriteRule ^/TYPO3root/(typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
RewriteRule ^(typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]

# If the file/symlink/directory does not exist => Redirect to index.php.
# For httpd.conf, you need to prefix each '%{REQUEST_FILENAME}' with '%{DOCUMENT_ROOT}'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

# Main URL rewriting.
# For httpd.conf, use this line instead of the next one:
# RewriteRule .* /TYPO3root/index.php [L]
RewriteRule .* index.php [L]

</IfModule>

### End: Settings for mod_rewrite ###


### Begin: PHP optimisation ###

# If you do not change the following settings, the default values will be used.

# TYPO3 works fine with register_globals turned off.
# This is highly recommended, if your web server has it turned on.
##php_flag register_globals off

### End: PHP optimisation ###



### Begin: Miscellaneous ###

# Make sure that directory listings are disabled.
#Options -Indexes

### End: Miscellaneous ###


# Add your own rules here.
# ...

RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} ^www\.jobzzone\.de$ [NC]
RewriteRule ^(.*)$ https://www.jobzzone.de/$1 [R=301,L]

RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} ^bad-kreuznach\.jobzzone\.de$ [NC]
RewriteRule ^(.*)$ https://bad-kreuznach.jobzzone.de/$1 [R=301,L]

RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} ^birkenfeld\.jobzzone\.de$ [NC]
RewriteRule ^(.*)$ https://birkenfeld.jobzzone.de/$1 [R=301,L]

RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} ^mainz-bingen\.jobzzone\.de$ [NC]
RewriteRule ^(.*)$ https://mainz-bingen.jobzzone.de/$1 [R=301,L]

# Umleitung von alter www.jobzzone.de nach bad-kreuznach.jobzzone.de, aber nur für das /unternehmen Unterverzeichnis
#               funktioniert noch nicht!


<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.jobzzone\.de$ [NC]
RewriteRule ^jobzzone.dievorschau.de/unternehmen/(.*)$ http://jobzzone-badkreuznach.dievorschau.de/unternehmen/$1 [R=302,L]
RewriteRule ^jobzzone.dievorschau.de/unternehmen$      http://jobzzone-badkreuznach.dievorschau.de/unternehmen/   [R=302,L]
</IfModule>

编辑

我在htaccess中添加了以下内容,

RewriteCond %{HTTP_HOST} "^jobzzone.dievorschau.de$"     [NC,OR]
RewriteCond %{HTTP_HOST} "^www.jobzzone.dievorschau.de$" [NC]
RewriteRule "^/unternehmen/(.*)$"    "http://jobzzone-badkreuznach.dievorschau.de/unternehmen/"    [R=302,L]

RewriteCond %{HTTP_HOST} "^jobzzone.dievorschau.de$"     [NC,OR]
RewriteCond %{HTTP_HOST} "^www.jobzzone.dievorschau.de$" [NC]
RewriteRule "^/unternehmen/(.*)$"    "http://jobzzone-badkreuznach.dievorschau.de/unternehmen/$1"  [R=302,L]

但是重定向仍然无效。

Redacted:~ redacted$ wget "http://jobzzone.dievorschau.de/unternehmen/arbeitsagentur" --server-response
-2018-10-04 09:07:57--  http://jobzzone.dievorschau.de/unternehmen/arbeitsagentur
Auflösen des Hostnamens jobzzone.dievorschau.de (jobzzone.dievorschau.de)… 2a03:2a00:1200:0:1::3795, 37.202.5.54
Verbindungsaufbau zu jobzzone.dievorschau.de (jobzzone.dievorschau.de)|2a03:2a00:1200:0:1::3795|:80 … verbunden.
HTTP-Anforderung gesendet, auf Antwort wird gewartet … 
 HTTP/1.1 301 TYPO3 RealURL redirect
 Date: Thu, 04 Oct 2018 07:07:58 GMT
 Server: Apache
 X-TYPO3-RealURL-Info: redirect for missing slash
 Connection: close
 Upgrade: h2,h2c
 Connection: Upgrade
 Content-length: 0
 Location: http://jobzzone.dievorschau.de/unternehmen/arbeitsagentur/
 Content-Type: text/html; charset=UTF-8
Platz: http://jobzzone.dievorschau.de/unternehmen/arbeitsagentur/ [folgend]
-2018-10-04 09:07:58--  http://jobzzone.dievorschau.de/unternehmen/arbeitsagentur/
erbindungsaufbau zu jobzzone.dievorschau.de (jobzzone.dievorschau.de)|2a03:2a00:1200:0:1::3795|:80 … verbunden.
TTP-Anforderung gesendet, auf Antwort wird gewartet … 
 HTTP/1.1 404 Not Found
 Date: Thu, 04 Oct 2018 07:07:58 GMT
 Server: Apache
 Upgrade: h2,h2c
 Connection: Upgrade, Keep-Alive
 Keep-Alive: timeout=5, max=100
 Transfer-Encoding: chunked
 Content-Type: text/html; charset=UTF-8
2018-10-04 09:07:58 FEHLER 404: Not Found.

在TYPO3安装中安装了RealUrl,并附加了丢失的斜杠和重定向,但是htaccess重定向似乎没有任何作用...

1 个答案:

答案 0 :(得分:0)

由于您无权修改VirtualHost,因此可以在.htaccess文件中这样做:

RewriteEngine On

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

RewriteCond %{HTTP_HOST} "^test\.de$" [NC,OR]
RewriteCond %{HTTP_HOST} "^www.test\.de$" [NC]
RewriteRule "^/subdirectory$"    "http://subd.example.com/"   [R=301,L]

这样,重定向仅应用于域test.dewww.test.de

对于这两个(或选项),如果有人要求/subdirectory/SOMETHING/subdirectory,它将重定向到该子域。


根据我的回答在您的评论后的示例

您要

http://jobzzone.dievorschau.de/unternehmen/*
    redirected to
http://jobzzone-badkreuznach.dievorschau.de/unternehmen/(the value of *)

您提供的示例:

http://jobzzone.dievorschau.de/unternehmen/beinbrech 
    needs to redirect to 
http://jobzzone-badkreuznach.dievorschau.de/unternehmen/beinbrech

配置变为:

RewriteEngine On

RewriteCond %{HTTP_HOST} "^jobzzone.dievorschau.de$"     [NC,OR]
RewriteCond %{HTTP_HOST} "^www.jobzzone.dievorschau.de$" [NC]
RewriteRule "^/unternehmen/(.*)$"    "http://jobzzone-badkreuznach.dievorschau.de/unternehmen/"    [R=301,L]

RewriteCond %{HTTP_HOST} "^jobzzone.dievorschau.de$"     [NC,OR]
RewriteCond %{HTTP_HOST} "^www.jobzzone.dievorschau.de$" [NC]
RewriteRule "^/unternehmen/(.*)$"    "http://jobzzone-badkreuznach.dievorschau.de/unternehmen/$1"  [R=301,L]