www.webdeveloper.com
Results 1 to 3 of 3

Thread: mod_rewrite anchor

  1. #1
    Join Date
    Feb 2010
    Posts
    7

    mod_rewrite anchor

    Hi guys. I am writing a page, and would like to use mod_rewrite to make the URLs simpler with anchors. For example, i need to get lorem.com/1 to rewrite with an anchor to lorem.com/#ipsum and lorem.com/2 to rewrite to lorem.com/#dolor, etc.

  2. #2
    Join Date
    Dec 2008
    Posts
    13
    Oh well, that's gonna take you a lot of work to map all your urls. Fortunately, Apache has this neat little feature called RewriteMap (though it doesn't work in .htaccess, only in virtual hosts or main server config, and it requires you to know the filesystem).

    Code:
    RewriteEngine On
    RewriteMap map txt:/path/to/map.txt
    RewriteCond %{HTTP_HOST} ^lorem.com [NC]
    RewriteCond %{REQUEST_URI} ^#
    RewriteCond ${map:$1} >"" [NC]
    RewriteRule ^/#(.*) /${map:$1}
    And in your map.txt:
    Code:
    #source destination
    ipsum 1
    dolor 2
    etc
    (For more information, see http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html.)

    Or do it the classical way, mapping manually:
    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^lorem.com [NC]
    RewriteRule ^/#ipsum$ http://lorem.com/1
    RewriteRule ^/#dolor$ http://lorem.com/2
    etc
    (untested)
    Last edited by DeatzoSeol; 04-21-2010 at 07:14 PM. Reason: Woops. Typo

  3. #3
    Join Date
    Dec 2008
    Posts
    13
    Wait, did you mean map lorem.com/1 to lorem.com/#ipsum? That doesn't make sense IMO but you can simply reverse the key-value pairs in your map and change the rewriterule to
    Code:
    RewriteEngine On
    RewriteMap map txt:/path/to/map.txt
    RewriteCond %{HTTP_HOST} ^lorem.com [NC]
    RewriteCond %{REQUEST_URI} ^/[1-9]+
    RewriteCond ${map:$1} >"" [NC]
    RewriteRule ^/(.*) /#${map:$1}
    Also, be wary about anchor rewriting in general, as the anchors on your index page will become quite useless.
    Last edited by DeatzoSeol; 04-21-2010 at 07:00 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles