大网数据广告活动
网站运营与故障问答

How to remove .jsp extension and replace with forward Slash in htaccess?

· 52CE 我爱测

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 %{REQU…

问题原文

原帖链接 · Vaibhav Singh · CC BY-SA 3.0

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.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

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?

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-09-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。