3

Here is my actual URL

http://www.getinfotowin.com/virtual/PageRouteone?actionName=best_television_Series&service=T&id=50&customerId=81&KeyId=1&IsVisible=N&service=Nothing

Expected short URL

http://www.getinfotowin.com/best_television_Series/T/50/81/1/N/Nothing

My MOD_REWRITE logic is as given below

RewriteRule    ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$    

virtual/PageRouteone?actionName=$1&service=$2&id=$3&customerId=$4&KeyId=$5&IsVisible=$6&service=$7 [NC,L]    # Process product requests

Our apache communicates with Tomcat using AJP protocal. I have tried the above given logic but it's not working. In the actual URL "virtual" is my war file name and "PageRouteone" is my java servlet name.

I want to know whether or not my rewrite rule is correct. If not, what is it that's wrong?

Welz
  • 236
  • 2
  • 10
  • 21
tina
  • 312
  • 1
  • 4
  • 18
  • Which mvc are you using? – Stefan Jun 30 '14 at 19:02
  • below lines are what i get in my rewrite logs init rewrite engine with requested uri /virtual/PageRouteone applying pattern '^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$' to uri '/virtual/PageRouteone' pass through /virtual/PageRouteone – tina Jul 04 '14 at 10:49
  • @Stefoan we are using apache and tomcat servers in a clustered environment jsp is our view java class acts as model and controller – tina Jul 04 '14 at 10:52
  • Even if you get this regex right, it's going to be slow and painful to maintain & debug (like if you need to add more parameters). You might think about parsing that URL server side. For instance, you could set up a Servlet Filter to split the URL and set each section as a request parameter. – Cameron Jul 07 '14 at 14:58
  • @tina Your rule simply not accept underscores. See my answer for details. – zessx Jul 11 '14 at 08:01

3 Answers3

0

You anchor your pattern to the beginning of the string (the URI path), but the start of the pattern does not match the '/' character that will always start the URI path. This pattern would probably work better:

^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$

Since you are using the NC option for case-insensitive matching, you should be able to reduce that to:

^/([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/?$

You could even consider simplifying it to this (in which case NC will no longer be relevant):

^/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$

That will pick out each path segment regardless of the characters in it, leaving validation of the parameters to your application.

In any case, you may need to use the PT (passthrough) option to get AJP to claim the rewritten URL (but test, because it's more efficient to not passthrough).

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Sources: Apache HTTPD 2.4 documentation, specifically http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule (specifying that the (first) rule matches against URI(URL) path, and documenting the RewriteRule options), and http://httpd.apache.org/docs/current/mod/directive-dict.html#Syntax (defining URL path). – John Bollinger Jul 08 '14 at 19:04
0

I think your RewriteRule is almost correct, but I would remove the last question mark and the last slash. Make sure that the RewriteEngine is on.

RewriteEngine On
RewriteBase /
Options -MultiViews

RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ virtual/PageRouteoneactionName=$1&service=$2&id=$3&customerId=$4&KeyId=$5&IsVisible=$6&service=$7 [NC,L]

In your code use your provided URL:

http://www.getinfotowin.com/best_television_Series/T/50/81/1/N/Nothing

NOTE: URLs with a slash will not work. You have to define another rule for that.

If you want that URLs with a trailing slash are also working, you have to add following lines:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)$
RewriteRule ^(.+)/$ http://www.domain.com/$1 [R=301,L]
GuyT
  • 4,316
  • 2
  • 16
  • 30
0

Solution:

Your URL contains underscores (_), which are not accepted by your pattern [A-Za-z0-9-]+.

Change it for [\w-]+ (which is equal to [A-Za-z0-9_-]+).

Here is your final rule :

RewriteRule 
    ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ 
    virtual/PageRouteone?actionName=$1&service=$2&id=$3&customerId=$4&KeyId=$5&IsVisible=$6&service=$7 
    [NC,L]    # Process product requests

Side note:

It should be obvious, but I suspect a misunderstanding : you must use the short URL everywhere, this rule will redirect the short (virtual) URL to the servlet with some parameters, but don't expect it to shorten your URL automatically.

I wanted to clarify this point because this is your first question, and many people on SO think this module is here to beautify the (full) URLs they've used everywhere.

zessx
  • 68,042
  • 28
  • 135
  • 158