0

I am trying to remove trailing-slash from one of my website and it works but want to keep trailing-slash on a few URLs and want to remove from all remaining. The website content is accessible based on the region you are in or the region you choose.

The website has region-specific setup, below is the website structure setup example

India - example.com/in/ US - example.com/us/ UAE - example.com/ae/ International - example.com

and other pages like

example.com/in/products
example.com/us/blogs
example.com/product

I want to keep trailing slash after regions like example.com/us/, example.com/uk/

but want to remove from all remaining URLs like

example.com
example.com/us/products
example.com/us/blogs
example.com/blogs

My existing code removes trailing slashes from all URL

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

How to make this possible in codeigniter 3 setup?

  • Your current RewriteCond is quite superfluous to begin with - it tests pretty much the same thing you are testing with the RewriteRule pattern anyway. Replace it with a negated condition that checks that the REQUEST_URI is not one of your region prefixes only. – CBroe Jun 26 '23 at 10:00

1 Answers1

0

You can use conditional statements in your .htaccess to accomodate the different regions.

RewriteEngine On

# Remove trailing slash from URLs except for region-specific URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(in|us|ae)/ [NC]
RewriteRule ^(.+)/$ /$1 [L,R=301]

# Add trailing slash to region-specific URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(in|us|ae)/ [NC]
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

The first set of rules removes the trailing slash from URLs that are not region-specific. It checks if the request is not for an existing directory and if the request URI does not start with /in/, /us/, or /ae/. If both conditions are met, the trailing slash is removed using a 301 redirect.

The second set of rules adds a trailing slash to region-specific URLs. It checks if the request is not for an existing directory and if the request URI starts with /in/, /us/, or /ae/. If both conditions are met, a trailing slash is added using a 301 redirect.

James
  • 834
  • 7
  • 27