Mobile app version of vmapp.org
Login or Join
Correia994

: How to remove .jsp extension and replace with forward Slash in htaccess? I want to remove the .jsp extension from my URL and replace it with a forward slash eg. example.com/xyz.jsp to example.com/xyz/

@Correia994

Posted in: #Htaccess #Url #UrlRewriting

I want to remove the .jsp extension from my URL and replace it with a forward slash eg. example.com/xyz.jsp to example.com/xyz/ and example.com/xyz.jsp?ab=12 to example.com/xyz/?ab=12.

I used:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ .php
RewriteRule ^([^/]+)/([^/]+)/$ //.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ // [R=301,L]


And

RewriteEngine On
RewriteBase /

# external redirect from /example.html to /example
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/+([^.]+).html [NC]
RewriteRule ^ /%1/ [R=301,L]

# internal forward from /example/ to //example.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/.html -f
RewriteRule ^(.+?)/?$ /.html [L]


In my .htaccess but neither are not working correctly.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Correia994

1 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

You will need to ensure that MultiViews is disabled before this will work correctly, as this will tend to conflict with your mod_rewrite directives. Add this in your .htaccess file:

Options +FollowSymLinks -MultiViews


(FollowSymLinks needs to be enabled for mod_rewrite to work, so just to be sure.)

Then, something like what you already have looks reasonable:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/.jsp -f
RewriteRule (.+)/$ .jsp [L]


I've made the trailing slash mandatory on the URL (otherwise you potentially have two URLs accessing the same content - duplicate content).

UPDATE: To redirect any requests to the .jsp URL to the canonical URL (ie. without the extension and with a trailing slash) then something like the following (similar to what you had in your question) would need to go before the directives above:

RewriteCond %{THE_REQUEST} .jsps
RewriteRule (.+).jsp$ // [R=301,L]


This is only strictly necessary if the .jsp URLs had been indexed or externally linked to. If this is a new site then this step is optional.

It is more efficient to match what you can with the RewriteRule pattern (ie. (.+).jsp$), rather than have a catch-all regex here. The THE_REQUEST condition ensures that this only applies to initial requests and not rewritten requests - thus preventing a redirect loop.

So, in summary:

# Disable MultiViews
Options +FollowSymLinks -MultiViews

RewriteEngine On

# Remove file extension from URLs (external redirect)
RewriteCond %{THE_REQUEST} .jsps
RewriteRule (.+).jsp$ // [R=301,L]

# Internally rewrite extensionless URLs back to ".jsp"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/.jsp -f
RewriteRule (.+)/$ .jsp [L]




DEBUGGING: To help with debugging the above, add the following directive below the RewriteEngine On directive and check the environment variables (MOD_REWRITE_THE_REQUEST and MOD_REWRITE_URL_PATH) in your server-side code:

RewriteCond %{THE_REQUEST} (.*)
RewriteRule (.*) - [E=MOD_REWRITE_THE_REQUEST:%1,E=MOD_REWRITE_URL_PATH:]


What do these environment variables contain when you access a .jsp URL?

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme