10

I went through all the answered threads on rewrite rule problem. Tried the suggestions, But I still don't know what I am doing wrong.

I want to make a 301 redirect of our old URLs to the new ones. Example:

old url : http://www.xyz.com/abc/topics.html
new url : http://www.xyz.com/index.php#first

I am trying following rule in .htaccess:

RewriteEngine on
RewriteRule ^\/abc\/(.+)$ http://www.xyz.com/index.php#first [L,R=301]

Any advice is highly appreciated

LazyOne
  • 158,824
  • 45
  • 388
  • 391
mooglife
  • 226
  • 1
  • 2
  • 8

2 Answers2

23

Try this:

RewriteRule ^abc\/(.+)$ http://www.example.com/index.php#first [L,R=301,NE]
Community
  • 1
  • 1
Josep Valls
  • 5,483
  • 2
  • 33
  • 67
  • Thanks, this solution redirects to the destination page but in the url instead of '#' it displays '%23', thereby not scrolling to the anchor. – mooglife Oct 19 '11 at 09:55
  • 3
    found the solution, Had to use [NE] flag as well. so now it is: ^abc\/(.+)$ http://www.xyz.com/index.php#first [L,R=301,NE] – mooglife Oct 19 '11 at 10:18
  • 6
    Details: `NE` flag prevents special characters (such as #,?,&) from being converted to their hex codes. `L` prevents further rules from being executed, which in-turn prevents possible "Invalid Request" warnings. See: https://httpd.apache.org/docs/2.4/rewrite/flags.html – musicin3d Jun 12 '18 at 22:10
4

You can, of course, do it by "mod_rewrite", but in this situation I'd suggest you use mod_alias as it is faster and simpler (see this SO response: mod_rewrite or mod_alias?)

Like this:

Redirect permanent /abc http://www.xyz.com/index.php#first
Community
  • 1
  • 1
naivists
  • 32,681
  • 5
  • 61
  • 85
  • I went through mod_alias, seems like it is faster. but one drawback is: xyz.com/abc/test redirects to http://www.xyz.com/index.php#first/test – mooglife Oct 19 '11 at 09:56