W: H: YOffset??
Related Media
Categories   /  Tags

.htaccess special settings & route enhancers in TYPO3

Script snippets for individual redirects or to avoid BE login errors in Typo3 versions from V 11. Furthermore, special settings for CORS, X-Frame Options, Security-Headers, MIME-Types, extended file handling, nice urls due mod_rewrite for tx_news and various regular expressions.

Redirects & special settings

.htaccess file in website root.

Overview simple examples

 


# Aktivate Rewrite Engine
RewriteEngine On
# Settings for Error-Documents
ErrorDocument 404 /fehlerseite.php
ErrorDocument 500 /fehlerseite.php
# Settings for PHP
AddHandler application/x-httpd-php .php
php_value upload_max_filesize 64M
php_value post_max_size 64M
# Settings for file-& directory access
Options -Indexes
  deny from all
# Setting the Standard-Indexfile
DirectoryIndex index.html index.htm index.php
# Redirects
Redirect 301 /alte-seite.html /neue-seite.html
RewriteRule ^alte-seite2\.html$ /neue-seite2.html [R=301,L]

 

Adding a custom header and value

 

Header set X-Custom "Custom Value"

 

Blocking users based on IP

 

order allow,deny
deny from 255.x.x.x
deny from 123.x.x.x
allow from all

 

Blocking referrers (hotlink protection)
Blocking referrers, also known as hotlink protection, is a method used to block certain referrers from referencing your website's assets and thus stealing your bandwidth. Use the snippet below to define which domains aren't allowed to refer to your content and thus they will receive a 403 Forbidden error

 

RewriteCond %{HTTP_REFERER} unwanteddomain\.com [NC,OR]
RewriteCond %{HTTP_REFERER} unwanteddomain2\.com
RewriteRule .* - [F]

 

Adding MIME types
To see a full list of MIME types visit the MIME Types List.

 

AddType image/gif .gif .GIF

 

Leveraging browser caching

 

## EXPIRES CACHING ##
    ExpiresActive On
    ExpiresByType image/jpg "access 1 year"
    ExpiresByType image/jpeg "access 1 year"
    ExpiresByType image/gif "access 1 year"
    ExpiresByType image/png "access 1 year"
    ExpiresByType text/css "access 1 month"
    ExpiresByType text/html "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access 1 month"
## EXPIRES CACHING ##

 

Enabling CORS

 

<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|font.css|css|js|gif|png|jpe?g|svg|svgz|ico|webp)$">
Header set Access-Control-Allow-Origin “*” </FilesMatch>
</IfModule>

 

Redirection from http to https taking into account the subdomain (www.)

 

#.htaccess
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ www.domain.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ www.domain.com/$1 [R=301,L]
</IfModule>
#The redirect now works as follows:
#http://www.domain.de will be redirected to www.domain.de.
#http://domain.de will be redirected to -https://www.domain.de.
#https://domain.de will be redirected to -https://www.domain.de.

 

 

TYPO3: Route enhancers focusing tx_news in /config/sites/xyz/config.yaml

Example for two language system and nice urls in tx_news ( list, detail, category, tags ) and ke_search.
TYPO3 11 & TYPO3 12 tested.

 


base: /
languages:
  -
    title: English
    enabled: true
    languageId: 0
    base: /
    locale: en_US.UTF-8
    navigationTitle: English
    flag: us
    hreflang: ''
    websiteTitle: ''
  -
    title: German
    enabled: true
    locale: de_DE
    hreflang: de-DE
    base: /de/
    websiteTitle: 'your-domain.com'
    navigationTitle: German
    fallbackType: fallback
    fallbacks: '0'
    flag: de
    languageId: 1
rootPageId: 1
routeEnhancers:
  News:
    type: Extbase
    extension: News
    plugin: Pi1
    routes:
      -
        routePath: /
        _controller: 'News::list'
      -
        routePath: '/page-{page}'
        _controller: 'News::list'
        _arguments:
          page: currentPage
      -
        routePath: '/{news-title}'
        _controller: 'News::detail'
        _arguments:
          news-title: news
      -
        routePath: '/{category-name}'
        _controller: 'News::list'
        _arguments:
          category-name: overwriteDemand/categories
      -
        routePath: '/{tag-name}'
        _controller: 'News::list'
        _arguments:
          tag-name: overwriteDemand/tags
    defaultController: 'News::list'
    defaults:
      page: '0'
    aspects:
      news-title:
        type: PersistedAliasMapper
        tableName: tx_news_domain_model_news
        routeFieldName: path_segment
      page:
        type: StaticRangeMapper
        start: '1'
        end: '100'
      category-name:
        type: PersistedAliasMapper
        tableName: sys_category
        routeFieldName: slug
      tag-name:
        type: PersistedAliasMapper
        tableName: tx_news_domain_model_tag
        routeFieldName: slug
  PageTypeSuffix:
    type: PageType
    map:
      feed.xml: 9818
      calendar.ical: 9819
websiteTitle: your-domain.com