Looking for guidance rewriting specific URL's

I have changed the CMS to my site and it has rewritten the URLs to be more friendly but I have 20 pages which are linked on different sites across the web. I want when people click those old links for them to be redirected to their new respective posts.

I was at first thinking of using a PHP array with a javascript redirect but now I'm thinking .htaccess mod-rewrite would be best.

Whats the best way to have specific URLs redirect to their new URLs?


Use the .htaccess file for redirects. For example:

# This allows you to redirect index.html to a specific url
Redirect /index.html http://example.com/location/

Add some regex to match your rewrites and send to the dynamically created remote url. For example:

# Dynamic redirect
RedirectMatch 301 yourUrl/\(.*)\$ http://external.com/someUrl.html?dynamicVal=$1

That would send ...yourUrl/123 -> http://external.com/someUrl.html?dynamicVal=123

If you are talking about URLs on your pages, then the best way is to edit all such pages with current proper URLs.

If you are talking about "what to do when someone hits my old URL (from their bookmarks or by clicking a link on another site, for example) then the best way is to redirect (with code 301) such requests to a new location using Apache's URL Rewrite module.


# Activate Rewrite Engine
RewriteEngine On

# redirect to another page on the same site
RewriteRule ^mypage.php /other/mypage.php [R=301,L,QSA]

# redirect to another page on ANOTHER site
RewriteRule ^mypage.php http://www.anothersite.com/mypage.php [R=301,L,QSA]

Here is the link to Apache's manual for mod-rewrite.

Another useful place -- Mod_Rewrite Forums.

Apache supports different modules to rewrite/redirect requested URIs:

  • mod_alias with Alias and AliasMatch for internal rewriting, as well as Redirect and RedirectMatch for external redirecting
  • mod_rewrite with RewriteRule for internal rewriting and external redirecting

The main difference between the mod_alias and mod_rewrite directives is that mod_alias’s Alias and Redirect directives work with path prefix patterns while the other directives (i.e. AliasMatch, RedirectMatch, and RewriteRule) work with regular expressions.

A path prefix pattern is always always matched when it’s a qualified prefix of the requested path and the remaining path segments are automatically appended to the target path. Here’s an example:

Redirect /foo/bar http://other.example/quux

If /foo/bar is requested, it’s getting redirected to http://other.example/quux; likewise /foo/bar/baz is getting redirected to http://other.example/quux/baz. Alias works alike but just internally.

In opposite to that, AliasMatch, RedirectMatch, and RewriteRule work with regular expressions and can be used not just to match path prefixes. Here’s an example:

RedirectMatch ^/foo/bar$ http://other.example/quux

If /foo/bar is requested, it’s getting redirected to http://other.example/quux (just like the Redirect example above). But if /foo/bar/quux is requested, it’s not getting redirected as ^/foo/bar$ doesn’t match /foo/bar/quux.

Futher differences between mod_alias and mod_rewrite are that mod_rewrite can also examine other parts of the URI except the path and even HTTP request header fields. It does also allow more complex operations with multiple conditions.

Need Your Help

Visual Studio Long wait before Starting to build

c# visual-studio-2010 build

We have a moderately sized solution, with about 20 projects. In one of them I have my business entities. On compiling any project, visual studio waits and hangs about one and a half minutes on this

Want to change field in yii from

jquery yii

What I want to know is that, I want to change textField or dropDownList on dropDownlist change using jQuery.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.