Search code examples
apache.htaccesswebserver

Last pathname redirecting to first pathname if it's a document


Let's say I have a document called contact.php; I noticed for some reason when I put a string like this into the URL bar I always get redirected to the initial document (if it exists):

https://www.website.com/contact/pathname2

But if the URL is like this it goes to an error 300 page (expected behavior):

https://www.website.com/Contact/pathname2

How do I get the same result [of the expected behavior] for both URL entries regardless of case sensitivity? Unless that page exists (see line 18 of .htaccess code below)

Here's what I put in the .htaccess

RewriteEngine On
RewriteBase /

RewriteCond %{SERVER_PORT} 80

CheckSpelling On

# Always https
RewriteRule ^(.*)$ https://www.website.com/$1 [R,L]

# Redirect pages
RewriteRule ^Home/?$ index.php [QSA,NC]
RewriteRule ^Inventory/?$ inventory.php [QSA,NC]
RewriteRule ^Shows/?$ shows.php [QSA,NC]
RewriteRule ^Contact/?$ contact.php [QSA,NC]

# Change URL to .../Inventory/SKU(number)
RewriteRule ^Inventory/([^/]+)/?$ /vendors/pages/Inventory/LandingPage/DirectSKU.php?number=$1 [QSA,L,NC]

# Check if page has error
ErrorDocument 300 /vendors/pages/error.php
ErrorDocument 400 /vendors/pages/error.php
ErrorDocument 401 /vendors/pages/error.php
ErrorDocument 403 /vendors/pages/error.php
ErrorDocument 404 /vendors/pages/error.php
ErrorDocument 500 /vendors/pages/error.php

Solution

  • With your shown attempted/rules could you please try following Rules file. Please make sure to clear your browser cache before testing your URLs. Your rules looks like exact URLs which are ending with Contact(case insensitive) hence its not picking anything after Contact in URI. Also you haven't used L flag so you have to use it to stop it once a match of condition is found.

    Options -MultiViews
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{SERVER_PORT} 80
    
    CheckSpelling On
    
    # Always https
    RewriteRule ^ https://www.website.com%{REQUEST_URI} [NE,R=301,L]
    
    # Redirect pages
    RewriteRule ^Home index.php [QSA,NC,L]
    RewriteRule ^Inventory inventory.php [QSA,NC,L]
    RewriteRule ^Shows shows.php [QSA,NC,L]
    RewriteRule ^Contact contact.php [QSA,NC,L]
    
    # Change URL to .../Inventory/SKU(number)
    RewriteRule ^Inventory/([^/]+)/?$ /vendors/pages/Inventory/LandingPage/DirectSKU.php?number=$1 [QSA,L,NC]
    
    # Check if page has error
    ErrorDocument 300 /vendors/pages/error.php
    ErrorDocument 400 /vendors/pages/error.php
    ErrorDocument 401 /vendors/pages/error.php
    ErrorDocument 403 /vendors/pages/error.php
    ErrorDocument 404 /vendors/pages/error.php
    ErrorDocument 500 /vendors/pages/error.php