Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

htaccess redirect problem

8:00 pm on May 18, 2010 (gmt 0)

5+ Year Member

I have changed my dynamic url to static url which is working fine when the static url is used but I want to all those earlier dynamic url redirect automatically where I'm getting problem

dynamic url: ?a=read&page=test
static url: /articles/test.html

code I used for internal rewrite-

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^articles/([^/]*)\.html$ /index.php?a=read&page=$1 [L]
RewriteRule ^articles/$ /index.php?a=read&page=articles [L]
RewriteRule ^articles\.html$ /index.php?a=read&page=articles [L]
RewriteRule ^([^/]*)\.html$ /?a=$1 [L]

its working without any problem but can't make the old dynamic url redirect automatically to the new static url, tried separately by redirect, redirectmatch and rewrite rule but cant make it working :( -

Redirect 301 ?a=read&page=test /articles/$1.html
Redirect 301 http://www.example.com/?a=read&page=test http://www.example.com/articles/$1.html

RedirectMatch 301 ^a=read&page=(.+)$ /articles/$1.html

RewriteCond %{QUERY_STRING} ^a=read&page=(.*)$
RewriteRule ^articles/([^/]*)\.html$ /index.php?a=read&page=$1 [R=301,L]

here are the other codes of my htaccess file just in case anything needs to change -

IndexIgnore .htaccess */.?* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
order deny,allow
deny from all
AuthName www.example.com
AuthUserFile /home/example/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/example/public_html/_vti_pvt/service.grp

Options All -Indexes

Please help.... Thanks in advance
9:53 pm on May 18, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Options +FollowSymLinks
RewriteEngine On
# Externally redirect requests for dynamic URLs (now used only as filepaths) back to new static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(index\.php)?\?a=read&page=([^&\ ]+)\ HTTP/
RewriteRule ^(index\.php)?$ http://www.example.com/articles/%2\.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(index\.php)?\?a=([^&\ ]+)\ HTTP/
RewriteRule ^(index\.php)?$ http://www.example.com/%2\.html? [R=301,L]
# Internally rewrite requests for new static URLs to script filepath+query
RewriteRule ^articles(/|\.html)$ /index.php?a=read&page=articles [L]
RewriteRule ^articles/([^/.]+)\.html$ /index.php?a=read&page=$1 [L]
RewriteRule ^([^/.]+)\.html$ /index.php?a=$1 [L]

This demonstrates the use of the server variable THE_REQUEST to check to be sure that the dynamic script path is being directly requested by a client, and not as the result of one of previously executing one of your internal rewrites. THE_REQUEST is the original client request line exactly as sent to your server, and is what is logged in your raw server access logs, e.g.
GET /index.php?a=read&article=blue-widget-features HTTP/1.1

Note that this rewrite/redirect process is not symmetric or strictly reversible because your URLs were inconsistent; It is impossible to determine whether to redirect requests for "index.php?a=read&page=articles" to /articles/articles.html or to /articles/

I have chosen the former in the example code above, mainly because it eliminates a rewriterule and therefore improves efficiency.

I have also combined two of your internal-rewrite rules into one, and changed what was formerly your last rule to rewrite directly to index.php instead of to "/" to avoid the necessity to invoke mod_dir to do this -- again for efficiency.

The rules are now in the correct order to prevent unwanted exposure of filepaths as URLs, and to prevent unexpected operation by putting the rules within the two redirect- and rewrite- groups in order from most-specific to least- specific patterns and conditions.

Following on to this idea, should your wish to add redirects to canonicalize requests for "index.php" and/or for non-canonical domains, those two new rules would follow the first two redirects, in the order stated here.


[edit] Corrected as noted below. [/edit]

[edited by: jdMorgan at 12:27 am (utc) on May 19, 2010]

11:12 pm on May 18, 2010 (gmt 0)

5+ Year Member

Hi Thanks for helping but I'm still getting problem as when entering

its redirecting to-

missing the last variable!

tried also by -

which redirected to -
12:28 am on May 19, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Sorry, the "%1" in both of the first two rules should be "%2".

I corrected the code above to reflect this.


Featured Threads

Hot Threads This Week

Hot Threads This Month