Change .htaccess to redirect to a specific URL

3.3k views Asked by At

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.

This is my .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.

2

There are 2 answers

3
d0ug7a5 On

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 404 /path/to/errorPage.php

There's some more information about this within apache's documentation.

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.

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.

0
Raphael On

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).

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

Unfortunately the desired behaviour can't be achieved by adding a rule to your .htaccess file.

Since Shopware URLs are virtual, the rewrite rule in the default .htaccess file (that you have posted) directs all the requests to Shopware:

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

After that Shopware has to determine, if the URL is correct and the page is found. If the desired URL can't be found, Shopware sends a correct 404 status code, but displays the startpage.

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. Apache can neither use an ErrorDocument at this point after the PHP application (in this case Shopware) has processed the request.