How to redirect from URL with trailing slash to one without
-
30-01-2021 - |
Question
We are using the Redirect module 8.x-1.2 with Drupal 8.5.6. When generating URLs, we are finding that a trailing slash is added to the end of the URL, and this is creating a 503 error code.
How would I redirect the anonymous users from the URL with the trailing slash to the one without trailing slash?
Solution
Check your .htaccess file in root. There maybe a line that is overriding this rule. Check if this is in your .htaccess file, this forces a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L]
To remove the trailing slash use
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R]
If the Drupal setting is not being enforced then you may have a rule in your .htaccess file that is adding a slash. @usernameabc Try adding the last 2 lines under RewriteEngine on
OTHER TIPS
Select the Enforce clean and canonical URLs checkbox in admin/config/search/redirect/settings.
Enabling this will automatically redirect to the canonical URL of any page. That includes redirecting to an alias if existing, removing trailing slashes, ensure the language prefix is set and similar clean-up.
In my case, whenever I accessed a url, four slashes were automatically appended to the url (IP////<node_name
).
Following steps resolved my issue:
- Ensure that following entry is available in Apche configuration file - httpd.conf.
LoadModule rewrite_module modules/mod_rewrite.so
- Create a php file test.php with the following code.
<?php echo phpinfo(); ?>
- Open test.php in your browser. Check the Loaded Modules section to ensure that mod_rewrite is available.
- Also open the root Drupal PIP folder and ensure that .htaccess folder is there.