How to “cut” address - mod_rewrite
I have question about mod_rewrite.
I have links like this
http://pagename.com/pl/meskie/longsleevy/criminal-squad#ad-image-0
And I want to "cut" everything after # to get
http://pagename.com/pl/meskie/longsleevy/criminal-squad
I have many products in my shop so I don't want to create many rules for every product. So I created something like this:
RewriteRule ^/pl/meskie/longsleevy/([[^#]+)#([^#]+)$ ^/pl/meskie/longsleevy/([[^#]+)$
But this is not working. Can somebody help me?
My actual .htaccess
<IfModule mod_rewrite.c> SetEnv HTTP_MOD_REWRITE On RewriteEngine on RewriteBase / RewriteCond %{REQUEST_URI} !^/media/ RewriteCond %{REQUEST_URI} !^/extAdmin/ RewriteCond %{REQUEST_URI} !^/skin/ RewriteCond %{REQUEST_URI} !^/js/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule .* index.php </IfModule>
Thank You,
Answers
What about
RewriteRule ^/pl/meskie/longsleevy/([ˆ#]*)#.*$ /pl/meskie/longsleevy/$1
The brackets () on the left site will store everything before #, then print it with $1 on the right side. Also ^ and $ are useless on the right side.
Also, take care about the greedy .*
If you will change your mind and you want to rewrite url with fragment #, don't forget to add [NE] flag. See https://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_ne
Further reading apache docs