.htaccess: gängige index URL umleiten

Wolly300

Lt. Junior Grade
Registriert
Mai 2014
Beiträge
507
Hallo zusammen,

ich will alle gängie Formen der index urls umleiten.
Meine aktuelle .htaccess sieht wie folgt aus:

Code:
## Mod_rewrite in use.
RewriteEngine On

# Redirect .html URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

Mit meinem bisherigen wissen habe ich folgendes probiert:
Code:
## Mod_rewrite in use.

RewriteEngine On

# Redirect index
RewriteCond %{REQUEST_URI} ^(.*)index(\.php|\.html?)?$ [NC]
RewriteRule ^(.*)$ / [R=301,L]

# Redirect .html URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.html$ [NC]
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

Und wie fast zu erwarten hat es so nicht geklappt. Daher folgende Fragen:

1. Warum kann ich das NC Flag hier nicht verwenden?
Code:
RewriteCond %{REQUEST_URI} \.html$ [NC]

2. Wenn ich RewriteCond mit RewriteRule zusammen verwendet werden, was wählt dann RewriteRule aus?
Code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.html$ [NC]
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
Die Rule selectiert ja jetzt mit Regex alles vom String Start bis vor .html. Aber was ist der String start? Nennt mir bitte mal den Teil an z.B.: dieser URL:
Code:
https://analytics.google.com/analytics/web.html

3. Warum funktioniert dieser Redirect nicht?

Ich will URL's mit folgenden Endungen umleiten:
Code:
https://test.com/index.html
https://test.com/index.htm
https://test.com/index.php
https://test.com/index
auf diese URL:
Code:
https://test.com

Danke für eure Hilfe
 
https://www.htaccessredirect.net/

1. Geht sehrwohl! Ist ja nur ein "nocase".
2. + 3. https://webmasters.stackexchange.com/questions/119588/multiple-rules-to-rewrite-the-url Du musst ein [OR]-Operator anwenden in dem Fall deines Beispiels!
https://wiki.selfhtml.org/wiki/URL-Parameter

Apache-Konfiguration:
## Mod_rewrite in use.
RewriteEngine On

# Redirect all index Requests
RewriteCond %{REQUEST_URI} ^\/index$
RewriteRule .* https://test.com [R=301,L]

## Alternative (Explizit)
RewriteEngine On

# Redirect index URLs
RewriteCond %{REQUEST_URI} ^index(|\.[php|html|htm]\w+)$
RewriteRule .* https://test.com [R=301,L]
Also du kannst alles in eine RewriteCond packen in deinem Fall.

Apache-Konfiguration:
DirectoryIndex home.php
Deine eigentliche Einstiegsseite setzten nicht vergessen.
 
Zuletzt bearbeitet: (Ergänzung)
elgorro schrieb:
Also das klappt nicht.

1. Ein regex auf %{REQUEST_URI} muss mit "/" beginnen.
https://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet/#REQUEST_URI

2. Dein Regex filtert komischerweise auch index.ph oder index.ht . Ich checke aber noch nicht ganz wie das mit dem \w+ das auslöst.

1639837896755.png

https://regex101.com/r/NW3cXl/1

3. Ich habe es jetzt einfach mal mit diesem Command jetzt probiert (einfach zum Testen). Aber so werden alle URL's auf root umgeleitet. Dieses RewriteRule wird nicht nur bei der erfüllten RewriteCond, sondern immer ausgelöst. Aber warum?

Code:
 .htaccess
##
# @package    Joomla
# @copyright  Copyright (C) 2005 - 2020 Open Source Matters. All rights reserved.
# @license    GNU General Public License version 2 or later; see LICENSE.txt
##
##
# READ THIS COMPLETELY IF YOU CHOOSE TO USE THIS FILE!
#
# The line 'Options +FollowSymLinks' may cause problems with some server configurations.
# It is required for the use of Apache mod_rewrite, but it may have already been set by
# your server administrator in a way that disallows changing it in this .htaccess file.
# If using it causes your site to produce an error, comment it out (add # to the
# beginning of the line), reload your site in your browser and test your sef urls. If
# they work, then it has been set by your server administrator and you do not need to
# set it here.
##
## No directory listings
<IfModule mod_autoindex.c>
  IndexIgnore *
</IfModule>
## Suppress mime type detection in browsers for unknown types
<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
##
# Disable Federated Learning of Cohorts (FLoC)
# If you uncomment the below directive you have to allow this technology in the
# Global Configuration of Joomla. Read more about this in the Post-Installation
# message in the backend.
##
# Header always set Permissions-Policy "interest-cohort=()"
</IfModule>
## Can be commented out if causes errors, see notes above.
Options +FollowSymlinks
Options -Indexes
## Disable inline JavaScript when directly opening SVG files or embedding them with the object-tag
<FilesMatch "\.svg$">
  <IfModule mod_headers.c>
    Header always set Content-Security-Policy "script-src 'none'"
  </IfModule>
</FilesMatch>
## Mod_rewrite in use.
RewriteEngine On
# Redirect index
RewriteCond %{REQUEST_URI} ^\/index(\.php|\.html?)?$
RewriteRule ^(.*)$ https://%{SERVER_NAME} [R=301,L]
#RewriteCond %{REQUEST_URI} ^\/index(|\.[php|html|htm]\w+)$
#RewriteCond %{REQUEST_URI} ^\/index(\.php|\.html?)?$ [NC]
#RewriteRule ^(.*)$ https://%{SERVER_NAME}  [R=301,L]
# Redirect .html URL
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_URI} \.html$
#RewriteRule ^(.*)\.html$ /$1 [R=301,L]
## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site then comment out the operations listed
# below by adding a # to the beginning of the line.
# This attempts to block the most common type of exploit `attempts` on Joomla!
#
# Block any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root home page
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
## Begin - Custom redirects
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects
##
# Uncomment the following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##
# RewriteBase /
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
#
## End - Joomla! core SEF Section.
 
Zuletzt bearbeitet von einem Moderator:
Wolte mal gucken, ob du wach bist 👀

Apache-Konfiguration:
## Alternative (Explizit)
RewriteEngine On

# Redirect index URLs
RewriteCond %{REQUEST_URI} ^\/index(|\.(php|html|htm))$
RewriteRule .* https://test.com [R=301,L]

Zum Testen: https://htaccess.madewithlove.com/

Von Anfang an verstehe ich nicht, was du den bezwecken möchtest? Ob nun ne index.bla oder index.blub, das gibt doch kein "normaler" Benutzer in den Browser ein??

Einstiegspunkt (Document-Root) muss halt immer definiert sein, sonst wird der Standard genommen. Wenn du https://www.computerbase.de/ aufrufst, dann muss(!) ja ein Dokument die Seite zeigen, typischerweise index.php, index.html oder index.js ist also "/" (Root) physikalisch eben halt beispielsweise "/var/www/computerbase/index.php" - sonst wäre es ja quasi nichts (NULL) und man würde einen "Not Found" (Error 404) bekommen.
 
Zuletzt bearbeitet: (Document-Root)
elgorro schrieb:
Das ist bei CMS Systemen nicht ganz so. Da wird der Inhalt dynamisch generiert. z.B. habe ich eingestellt, das es keine .html Steiten gibt sondern nur /landingpage.

Da ich das ganze jetzt auch für Google noch weiter optimieren will. Will ich, das es für die Startseite nur noch die https://domain.de gibt.

Aktuell kann die Seite auch über inde.html, index.php, index erreicht werden. Diese will ich aber per 301 auf / domain dauerhaft umleiten.
Ergänzung ()

elgorro schrieb:
Also das was du mir geschickt hast, hätte klappen müssen, warum auch immer hat es nicht. So habe ich die .htaccess jetzt auch umgebaut. Aber entgegen jedem Verständnis und deiner Testseite greift die RewriteRule immer.



Code:
.htaccess
##
# @package    Joomla
# @copyright  Copyright (C) 2005 - 2020 Open Source Matters. All rights reserved.
# @license    GNU General Public License version 2 or later; see LICENSE.txt
##
##
# READ THIS COMPLETELY IF YOU CHOOSE TO USE THIS FILE!
#
# The line 'Options +FollowSymLinks' may cause problems with some server configurations.
# It is required for the use of Apache mod_rewrite, but it may have already been set by
# your server administrator in a way that disallows changing it in this .htaccess file.
# If using it causes your site to produce an error, comment it out (add # to the
# beginning of the line), reload your site in your browser and test your sef urls. If
# they work, then it has been set by your server administrator and you do not need to
# set it here.
##
## No directory listings
<IfModule mod_autoindex.c>
  IndexIgnore *
</IfModule>
## Suppress mime type detection in browsers for unknown types
<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
##
# Disable Federated Learning of Cohorts (FLoC)
# If you uncomment the below directive you have to allow this technology in the
# Global Configuration of Joomla. Read more about this in the Post-Installation
# message in the backend.
##
# Header always set Permissions-Policy "interest-cohort=()"
</IfModule>
## Can be commented out if causes errors, see notes above.
Options +FollowSymlinks
Options -Indexes
## Disable inline JavaScript when directly opening SVG files or embedding them with the object-tag
<FilesMatch "\.svg$">
  <IfModule mod_headers.c>
    Header always set Content-Security-Policy "script-src 'none'"
  </IfModule>
</FilesMatch>
## Mod_rewrite in use.
RewriteEngine On
# Redirect index
#RewriteCond %{REQUEST_URI} ^\/index(\.php|\.html?)?$ [NC]
#RewriteRule ^(.*)$ https://%{SERVER_NAME} [R=301,L]
#RewriteCond %{REQUEST_URI} ^\/index(|\.[php|html|htm]\w+)$
#RewriteCond %{REQUEST_URI} ^\/index(\.php|\.html?)?$ [NC]
#RewriteRule ^(.*)$ https://%{SERVER_NAME}  [R=301,L]
# Redirect index URLs
RewriteCond %{REQUEST_URI} ^\/index(|\.(php|html|htm))$
RewriteRule .* https://%{SERVER_NAME} [R=301,L]
# Redirect .html URL
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_URI} \.html$
#RewriteRule ^(.*)\.html$ /$1 [R=301,L]
## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site then comment out the operations listed
# below by adding a # to the beginning of the line.
# This attempts to block the most common type of exploit `attempts` on Joomla!
#
# Block any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root home page
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
## Begin - Custom redirects
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects
##
# Uncomment the following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##
# RewriteBase /
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
#
## End - Joomla! core SEF Section.

1639868571994.png


Das hätte jetzt nicht umgeleitet werden dürfen.
Bezüglich DirectoryIndex, das ist keine Pflicht Angabe und hat soweit auch keines meiner .htaccess Dateien. Diese werden defaul Mäßig von meinem CMS angelegt.
 
Zuletzt bearbeitet von einem Moderator:
Hinter den Landingpages ist aber trotzdem ne virtuelle Seite: Die https://domain.de/about ist ne index.html (.php oder .js ...) in /var/www/domain/about/index.html "grundsätzlich" (Alternativ gibt ja "Routing-Systeme die die $Request-URL benutzen und der Server dann weiterverarbeitet).

Spielt das wirklich so ne Rolle für den Google-Such-Algorhytmus ?

Danke für den Screenshot, da wollte ich gerade Fragen: Dann ist es tatsächlich so wie in diesem Beitrag erklärt: https://www.domain.de/kontakt (physikalisch oder virtuell: /var/www/domain/kontakt/index.html) wird getriggert von dem Regex (RewriteCond) und du kommst auf https.//www.domain.de raus, so wie in der RewriteRule angegeben.

Könnte evtl. der Code (Rewriting non-existing links to index.php) ganz unten weiterhelfen?
 
Zuletzt bearbeitet:
elgorro schrieb:
Das macht für mich aber trotzdem keinen Sinn.

Wenn ich die https://domain.de/kontakt(/index.html) aufrufe, sollte die RewriteCond nicht anspringen, weil:

Code:
RewriteCond %{REQUEST_URI} ^\/index(|\.(php|html|htm))$
RewriteRule .* https://%{SERVER_NAME} [R=301,L]

%{REQUEST_URI} das hier enthalten müssete "/kontakt/index.php" und wir eigentlich gesagt haben, dass es mit "/index" anfangen muss?
Ergänzung ()

elgorro schrieb:
Hinter den Landingpages ist aber trotzdem ne virtuelle Seite: Die https://domain.de/about ist ne index.html (.php oder .js ...) in /var/www/domain/about/index.html "grundsätzlich" (Alternativ gibt ja "Routing-Systeme die die $Request-URL benutzen und der Server dann weiterverarbeitet).

Spielt das wirklich so ne Rolle für den Google-Such-Algorhytmus ?

Danke für den Screenshot, da wollte ich gerade Fragen: Dann ist es tatsächlich so wie in diesem Beitrag erklärt: https://www.domain.de/kontakt (physikalisch oder virtuell: /var/www/domain/kontakt/index.html) wird getriggert von dem Regex (RewriteCond) und du kommst auf https.//www.domain.de raus, so wie in der RewriteRule angegeben.

Könnte evtl. der Code (Rewriting non-existing links to index.php) ganz unten weiterhelfen?
Ich habe mir den Code jetzt noch einmal genauer angeschaut und irgendwie macht dieser noch weniger Sinn.

Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Wenn ich Beispielsweise die Adresse https://domain.de/kontakt/index.php aufrufe, komme ich zu der 1. Rewrite Rule dort wird der String mit "^index\.php$" verglichen. Also ob der Test String mit index.php beginnt.
Aber was ist der Teststring (Welcher Teil der URL)?
 
Zuletzt bearbeitet:
Wolly300 schrieb:
"/index" anfangen muss?
Ob nun https://www.domain.de/index.html oder https://www.domain.de/kontakt/index.html kann nicht unterschieden werden. Deswegen Frage ich mich ja, was du bezweckst... Wenn du .php-Seiten hättest, dann könntest du .php aus der Regel rausnehmen, damit sie nicht gefiltert werden?!
Wolly300 schrieb:
Wenn ich Beispielsweise die Adresse https://domain.de/kontakt/index.php aufrufe, komme ich zu der 1. Rewrite Rule dort wird der String mit "^index\.php$" verglichen. Also ob der Test String mit index.php beginnt.
Aber was ist der Teststring (Welcher Teil der URL)?
RewriteRule: Vgl. https://httpd.apache.org/docs/2.4/rewrite/intro.html - URI-Requests (Also alles nach https://domain).
Code:
4. LoC (Line of Code): Wenn es eine index.php ist, dann mache nichts
5. LoC: Ist es keine reguläre Datei?
6. LoC: Ist es kein reguläres Verzeichnis (Landingpage)?
7. LoC: Wenn 5 oder 6 zutrifft, dann leite den User auf /index.php
 
elgorro schrieb:
Ob nun https://www.domain.de/index.html oder https://www.domain.de/kontakt/index.html kann nicht unterschieden werden. Deswegen Frage ich mich ja, was du bezweckst... Wenn du .php-Seiten hättest, dann könntest du .php aus der Regel rausnehmen, damit sie nicht gefiltert werden?!
Warum kann das nicht unterschieden werden?

URL: "https://domain.de/index.html" -> %{REQUEST_URI} = "/index.html"

URL: "https://domain.de/kontakt/index.html" -> %{REQUEST_URI} = "/kontakt/index.html"

Das sollte doch klappen?
 
Wenn du es unbedingt willst, dann müsstest du den Regex umschreiben: Negieren, sodass /index.html immer OK ist und alles andere nicht:
Wolly300 schrieb:
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>

Alles andere ist "zu Aufwändig": Du müsstest sonst beispielsweise expliziet sagen "kontakt", "support", "vertrieb" (...) ist OK und auch Pflegen. Es gibt Leute, die generieren sich sowas, für die Ladezeiten und Rechenleistung ist das aber nicht toll! Weil woher soll der Webserver selberwissen, was nicht sein darf?

Eine Kombo aus beiden kannst du dir natürlich selberbasteln (Ich kann sie dann auch gerne prüfen).
 
Zurück
Oben