简体   繁体   English

更改.htaccess以重定向到特定的URL

[英]Change .htaccess to redirect to a specific URL

I want to change my shopware .htaccess file to redirect to a specific URL when someone opens a site in my shop that doesn't exist. 当有人在我的商店中打开一个不存在的网站时,我想更改我的shopware .htaccess文件以重定向到特定的URL。

This is my .htaccess 这是我的.htaccess

    <IfModule mod_rewrite.c>
RewriteEngine on
#RewriteBase /shopware/

RewriteRule shopware.dll shopware.php
RewriteRule files/documents/.* engine [NC,L]
RewriteRule backend/media/(.*) media/$1 [NC,L]

RewriteCond %{REQUEST_URI} !(\/(engine|files|templates)\/)
RewriteCond %{REQUEST_URI} !(\/media\/(archive|banner|image|music|pdf|unknown|video)\/)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ shopware.php [PT,L,QSA]
</IfModule>

# Staging-Rules start
#SetEnvIf Host "staging.test.shopware.in" ENV=staging

DirectoryIndex index.html
DirectoryIndex index.php
DirectoryIndex shopware.php

# Disables download of configuration
<Files ~ "\.(tpl|yml|ini)$">
Deny from all
</Files>

# Enable gzip compression
<IfModule mod_deflate.c>
# disable compression on iconset due to loading problems in google chrome on windows
SetEnvIfNoCase Request_URI icon-set.css$ no-gzip dont-vary

AddOutputFilterByType DEFLATE text/html text/xml text/plain text/css text/javascript application/json
</IfModule>

<IfModule mod_expires.c>
<Files ~ "\.(jpe?g|png|gif|css|js)$">
ExpiresActive on
ExpiresDefault "access plus 1 month"
FileETag None
<IfModule mod_headers.c>
Header append Cache-Control "public"
Header unset ETag
</IfModule>
</Files>
</IfModule>

# Disables auto directory index
<IfModule mod_autoindex.c>
Options -Indexes
</IfModule>

<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>

<IfModule mod_php5.c>
# php_value memory_limit 128M
# php_value max_execution_time 120
# php_value upload_max_filesize 20M
php_flag phar.readonly off
php_flag magic_quotes_gpc off
php_flag session.auto_start off
php_flag suhosin.session.cryptua off
php_flag zend.ze1_compatibility_mode off
</IfModule>

# AddType x-mapp-php5 .php
# AddHandler x-mapp-php5 .php

I've searched a lot on google and tried some things but nothing worked fine for me. 我在Google上搜索了很多东西,并尝试了一些方法,但对我来说没有什么用。

I would probably use ErrorDocument here (provided that the state you are describing when you say a 'shop that doesn't exist' results in an HTTP Status of 404. For example: 我可能会在这里使用ErrorDocument (假设您说“不存在的商店”时所描述的状态导致HTTP状态为404。例如:

ErrorDocument 404 /path/to/errorPage.php

There's some more information about this within apache's documentation . apache的文档中有关于此的更多信息。

If you want a redirect based on conditions (in this case for instance the non-existence of a shop), then you could also experiment with using header() ( documentation is here ) to redirect, within the php - specifically the part that governs which page a user is taken to for instance. 如果您想基于条件进行重定向(在这种情况下,例如商店不存在),那么您也可以尝试在php中使用header()文档在此处 )进行重定向-具体来说就是例如,用户被带到哪个页面。

Do bear in mind you can't use header() once output to the browser has begun, as it will try to send a raw HTTP header, which can't be done if your script has already echo'd something to the browser. 请记住,一旦开始向浏览器输出,就不能使用header() ,因为它将尝试发送原始的HTTP标头,如果您的脚本已经向浏览器回显了某些内容,则无法执行此操作。

To achieve the desired behaviour as described by your question, you will have to install a Shopware plugin, that let's you specify a landing page, that will be displayed when a page is not found (an HTTP 404 error occurs within the shop). 为了实现您的问题所描述的理想行为,您将必须安装Shopware插件,让您指定登录页面,该登录页面将在找不到页面时显示(商店内发生HTTP 404错误)。

Fortunately there are plugins that you can use for that. 幸运的是,您可以使用一些插件。 You can find them in the Shopware community store, searching for the term "404": http://store.shopware.com/search?sSearch=404 您可以在Shopware社区商店中找到它们,并搜索术语“ 404”: http ://store.shopware.com/search?sSearch=404

Unfortunately the desired behaviour can't be achieved by adding a rule to your .htaccess file. 不幸的是,无法通过在.htaccess文件中添加规则来实现所需的行为。

Since Shopware URLs are virtual, the rewrite rule in the default .htaccess file (that you have posted) directs all the requests to Shopware: 由于Shopware URL是虚拟的,因此默认的.htaccess文件(您已过帐)中的重写规则会将所有请求定向到Shopware:

RewriteRule ^(.*)$ shopware.php [PT,L,QSA]

After that Shopware has to determine, if the URL is correct and the page is found. 之后必须确定Shopware,URL是否正确并找到页面。 If the desired URL can't be found, Shopware sends a correct 404 status code, but displays the startpage. 如果找不到所需的URL,Shopware将发送正确的404状态代码,但显示起始页。

Since only Shopware can determine, if the URL is found within it's system, Apache rewrite rules in .htaccess at this point won't work anymore. 由于只有Shopware可以确定,如果无法在其系统中找到URL,则此时.htaccess中的Apache重写规则将不再起作用。 Apache can neither use an ErrorDocument at this point after the PHP application (in this case Shopware) has processed the request. PHP应用程序(在本例中为Shopware)处理了请求之后,Apache此时无法使用ErrorDocument。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM