Home > Software engineering >  Redirect using .htaccess without trailing SLASH
Redirect using .htaccess without trailing SLASH

Time:09-10

I am long time reader and never had to ask a question. Mostly got answers just by reading this extraordinary forum. Recently I have been trying to resolve an issue with htaccess for several days now & it has kept me from progressing on my web project atm. Your help would be appreciated.

Server: OpenLiteSpeed

Current Set of Rules .htaccess (.htaccess resides in root directory)

RewriteEngine On
Options  FollowSymlinks -MultiViews -Indexes
DirectorySlash Off

RewriteBase /

#set default characterset from server end
AddDefaultCharset UTF-8

### Rewrite Rules Added by CyberPanel Rewrite Rule Generator
RewriteCond %{HTTP_HOST} ^www.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

### End CyberPanel Generated Rules.

### Rewrite Rules Added by CyberPanel Rewrite Rule Generator

RewriteCond %{HTTPS}  !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

### End CyberPanel Generated Rules.


#By blocking access from libwww-perl you can eliminate many simpler attacks
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]

## EXPIRES CACHING ##
# Add or remove file types if needed
ExpiresActive On
<FilesMatch "\.(jpg|jpeg|woff|woff2|png|gif|swf|ico|js|css|ttf|eot|ico|mp4|webm|svg|json|webp)$">
Header set Cache-Control "max-age=31536000, public, no-transform"
</FilesMatch>

## EXPIRES CACHING ##

#main landing page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ main.php [L]

The Issue

  1. I have a physical folder named: /packages/ residing in the root directory of my server.
  2. I would like to capture all queries in pattern of /packages & /packages/example/example/example to a file named: packages_main.php which also resides in the root dir.
  3. I have tried the following rules & more & variations
RewriteRule ^packages/(. ) package_main.php?qsa=$1 [L,QSA]
RewriteRule ^packages$ package_main.php [L,QSA]
RewriteRule ^packages package_main.php [L,QSA]
RewriteRule ^packages(. ) package_main.php [L,QSA]

**The problem is that whenever i open https://example.com/packages it gets redirected to https://example.com/packages/ ** Adding a trailing slash at the end.

I want it to stay like https://example.com/packages so that i can pass query strings to the page directly as well e.g. https://example.com/packages?p=10&a=11 but it redirects to https://example.com/packages/?p=10&a=11 which confuses my code.

I want to catch all urls except query strings which are to be passed to the package_main.php directly e.g. https://example.com/packages?p=100, https://example.com/packages/abc/bbc?p=100, https://example.com/packages/abc?p=100

This has taken 3 days of my work but i can't seem to figure it out. So basically i want a rule which redirects all my example.com/packages to package_main.php without adding a trailing slash. As the slash turns the query string as path for package_main.php to understand & it gets picked up incorrectly. Can someone help please.

CodePudding user response:

With your shown samples, attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.

RewriteEngine On
Options  FollowSymlinks -MultiViews -Indexes
DirectorySlash Off

RewriteBase /

#set default characterset from server end
AddDefaultCharset UTF-8

### Rewrite Rules Added by CyberPanel Rewrite Rule Generator
RewriteCond %{HTTP_HOST} ^www.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

### End CyberPanel Generated Rules.

### Rewrite Rules Added by CyberPanel Rewrite Rule Generator

RewriteCond %{HTTPS}  !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

### End CyberPanel Generated Rules.


#By blocking access from libwww-perl you can eliminate many simpler attacks
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]

## EXPIRES CACHING ##
# Add or remove file types if needed
ExpiresActive On
<FilesMatch "\.(jpg|jpeg|woff|woff2|png|gif|swf|ico|js|css|ttf|eot|ico|mp4|webm|svg|json|webp)$">
Header set Cache-Control "max-age=31536000, public, no-transform"
</FilesMatch>

## EXPIRES CACHING ##

RewriteCond %{QUERFY_STRING} ^$
RewriteRule ^packages/?(.*)?$ package_main.php?qsa=$1 [L,QSA]


#main landing page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ main.php [L]
  • Related