Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite module to handle site version folders (strip slash)

I'm using the following rule to use folders for different versions:

RewriteEngine On
RewriteRule .* - [E=VERSION:020]
RewriteRule ^versions/(.*)$ versions/$1 [L]
RewriteRule ^(.*)$ versions/%{ENV:VERSION}/$1 [L]

I have a structure like:

http://domain.com/versions/020/
http://domain.com/versions/020/th?=1
http://domain.com/versions/020/myfolder/
http://domain.com/versions/020/myfile.html

But the user can see:

http://domain.com/
http://domain.com/th?=1
http://domain.com/myfolder/
http://domain.com/myfile.html

I have a last problem with a slash that shows the version number.

A second entry point is (still working):

http://domain.com/site/

But the problem when the user type the url and omit the end forward slash, like this:

http://domain.com/site

The user now see the version number like this:

http://domain.com/versions/020/site/

This is a problem as I don't want the user to find out about other versions.

Any idea how I can solve the problem?

like image 213
Soundstep Avatar asked Nov 13 '22 16:11

Soundstep


1 Answers

It looks like this is interfering with mod_dir, which by default redirects the browser when it tried to access a directory and the trailing slash is missing. What's happening is the Rewrite is occuring, changing /site to /versions/020/site then mod_dir is redirecting the browser to http://domain.com/versions/020/site/. Thus the browser sees the versions/020 stuff in the location bar.

You can turn off mod_dir's automatic redirect with the "DirectorySlash Off" directive. But you may want to add the trailing slash anyways by including a redirect before any rewrites happen.

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

This or something equivalent before you do the rewrite to /version/020 should hopefully fix it.

Edit: Actually, now that I'm thinking about it, if you rewrite/redirect when trailing slash is missing, you don't need to turn off DirectorySlash because it'll always have a trailing slash by the time it gets to mod_dir. Maybe do it anyways to be safe?

like image 176
Jon Lin Avatar answered Dec 21 '22 23:12

Jon Lin