2

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 ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [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}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]

In my .htaccess but neither are not working correctly.

  • Just to confirm, presumably you have already changed the URL you are linking to in your application to read example.com/xyz/? The first code block references .php extensions and the second .html, whereas you are using .jsp - so even without looking at the code it's clear they are not going to work? But anyway, when you say "not working correctly", please be specific... what happens exactly, an errors? – MrWhite Sep 7 '16 at 7:59
  • i'm sorry, i just pasted from the site i referred, i've given .jsp in place of .html and .php .... the problem i'm facing is if i pass url by removing .jsp with / it's throwing 404 erro e.g www.mywebsite.com/sompage.jsp if i give url as www.mywebsite.com/sompage/ then it's not working. can u plz guide me over that? – Vaibhav Singh Sep 7 '16 at 15:05
1

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}/$1.jsp -f
RewriteRule (.+)/$ $1.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} \.jsp\s
RewriteRule (.+)\.jsp$ /$1/ [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} \.jsp\s
RewriteRule (.+)\.jsp$ /$1/ [R=301,L]

# Internally rewrite extensionless URLs back to ".jsp"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.jsp -f
RewriteRule (.+)/$ $1.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:$1]

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

|improve this answer|||||
  • I have made all the necessary changes in my .htaccess that you have told. Still me facing the same issue. – Vaibhav Singh Sep 8 '16 at 6:27
  • 1- My website is accessible with both .jsp and with forward slash. 2- After using this htaccess code all my internal links are not working. Check out this link faadoocoupons.com/natures-basket-coupons-offers-1584.jsp and faadoocoupons.com/natures-basket-coupons-offers-1584 for more clarification. – Vaibhav Singh Sep 8 '16 at 6:35
  • The errors that you are seeing (missing CSS, JS and incorrect internal links) are caused by relative links in your HTML, not becuase of the .htaccess rules above (which seem to be working OK). See this other question for more information on the relative path issue: webmasters.stackexchange.com/questions/86450/… – MrWhite Sep 8 '16 at 7:24
  • 1
    Thanks, That works great after using Base element in the Head section of my webpage. But My pages are accessible with both URL, Which can cause duplicate content problem. So how can i permanently redirect all my .jsp page to forward slash? – Vaibhav Singh Sep 8 '16 at 7:55
  • I've updated my answer with code to redirect from the .jsp URL to the canonical URL (ending in a slash). This is very similar to what you had initially, just with minor tweaks. – MrWhite Sep 8 '16 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.