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.
Printable View
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.
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).
And in your map.txt: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}
(For more information, see http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html.)Code:#source destination
ipsum 1
dolor 2
etc
Or do it the classical way, mapping manually:
(untested)Code:RewriteEngine On
RewriteCond %{HTTP_HOST} ^lorem.com [NC]
RewriteRule ^/#ipsum$ http://lorem.com/1
RewriteRule ^/#dolor$ http://lorem.com/2
etc
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
Also, be wary about anchor rewriting in general, as the anchors on your index page will become quite useless.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}