1

I'd like to rewrite my site url of below two patterns (note: there are a lot such urls that i hope to use one rule to rewrite them all)

www.hetaoblog.com/hello
www.hetaoblog.com/hello/

to

**www.hetaoblog.com/myblogs/post/hello.jhtml**

all other cases like http://www.hetaoblog.com/ should not be changed;

I've tried

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  #RewriteRule . /index.php [L]
  RewriteRule ^([^/]+)/$ /myblogs/post/$1.jhtml [L]
  RewriteRule ^(.*)$ /myblogs/post/$1.jhtml [L]
</IfModule>

and

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  #RewriteRule . /index.php [L]
  RewriteRule ^(.+)/$ /myblogs/post/$1.jhtml [L]
  RewriteRule ^(.*)$ /myblogs/post/$1.jhtml [L]
</IfModule>

and

RewriteRule ^([^/]+)+/$ /myblogs/post/$1.jhtml [L]

however, they work for my wanted two cases but they will both rewrite

www.hetaoblog.com/ 

to

www.hetaoblog.com/myblogs/post/.jhtml

It looks the + sign which should mean at least one occurrence is not happening.

2
  • Do you mean your client browser is getting a redirect? That's what it seems like is happening. Can you clarify what you're looking to accomplish - you're wanting to "rewrite" to www.hetaoblog.com/hello.jhtml, but your rules are pointing to /myblogs/post/$1.jhtml? Are you looking to redirect the client, or transparently serve content from the other location? Mar 13, 2012 at 1:00
  • I corrected my statement, i want to rewrite to www.hetaoblog.com/myblogs/post/hello.jhtml; i did not see a client redirect; the thing is, i only want urls like www.hetaoblog.com/hello and www.hetaoblog.com/hello/ to be rewritten; and keep www.hetaoblog.com/ unchanged; but now i saw in my tomcat that visiting www.hetaoblog.com/ also saw a url rewritten
    – hetaoblog
    Mar 13, 2012 at 1:50

3 Answers 3

2
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /myblogs/post/$1.jhtml [L]

Your / is redirecting since your RewriteCond lines don't apply to the second rule.

I'm still not sure how you determined how it was rewriting to /myblogs/post/.jhtml - that doesn't make sense at all with the rules you have, though.

Wordpress might still be interfering; turn on a RewriteLog location and set RewriteLogLevel 9 so that we can confirm that Apache's behaving properly.

3
  • thx, this works as expected! I put 'JKMount /*.jhtml ajp13' in apache's httpd.conf so that all *.jhtml request will be forward to my tomcat and I print the url in the servlet; btw, could you talk a bit more in detail about why my / is redirecting? i'm still confused. because it looks / is taking place at first rule. thx
    – hetaoblog
    Mar 13, 2012 at 11:44
  • Each RewriteCond line only applies to the RewriteRule that directly follows it. Normally, the %{REQUEST_FILENAME} !-d rule would prevent the subsequent rewrite for happening for /, since the DocumentRoot is (probably) a directory. But, that only applies to the first RewriteRule - the second pays no attention to the RewriteCond lines and simply rewrites if it matches. Since ^.*$ matches a zero-length string, it rewrites. The rule that I've provided for you avoids the need to make extra copies of the RewriteCond lines by handling the effect of both your RewriteRules in one. Mar 13, 2012 at 15:19
  • wow, amazing! thx a lot!
    – hetaoblog
    Mar 14, 2012 at 5:14
0

The simplest one is:

RewriteRule /hello/? /hello.jhtml [L]

This will match exactly the two URIs you specified and no others.

3
  • well, the thing is in this way i've to list all my possible pattern in the rewrite rule, which contains hundreds; because it was previously a wordpress blog with link pattern as hetaoblog.com/%post_name%, as i've many posts, so there are a lot
    – hetaoblog
    Mar 13, 2012 at 0:13
  • Can you describe a common pattern with all the URLs? Something like /blog/<number>/<title> would do.
    – Ladadadada
    Mar 13, 2012 at 0:16
  • current post url are all like /%post_name%/, such as /java-exception-handling/, /hello-world/; and i've other urls like /tool/hourlog.jhtml that will go to my tomcat; i hope to rewrite post urls to postname.jhtml so i'll let tomcat other than php to handle it
    – hetaoblog
    Mar 13, 2012 at 0:30
0

Have you tried simply:

RewriteRule ^/([A-Za-z0-9]+|[A-Za-z0-9]+/)$ /myblogs/post/$1.jhtml [L]

You also might need to specify rewritecond query string.

1
  • thx for help; the thing is i've chinese in the url that such a expression will not satify.
    – hetaoblog
    Mar 14, 2012 at 5:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .