Redirect old url to new url htaccess
To redirect an old URL to a new URL using .htaccess
file, you can use the Redirect
or RedirectMatch
directive. Here are some examples:
Redirect a specific old URL to a new URL
Redirect 301 /old-url http://example.com/new-url
This will redirect any requests to /old-url
to http://example.com/new-url
.
Redirect all URLs with a specific pattern to a new URL
RedirectMatch 301 ^/old-pattern http://example.com/new-url
This will redirect any URLs that match the pattern /old-pattern
to http://example.com/new-url
.
Redirect all URLs from an old domain to a new domain
Redirect 301 http://old-domain.com http://new-domain.com
This will redirect any requests from http://old-domain.com
to http://new-domain.com
.
Redirect all URLs from an old subdomain to a new subdomain
Redirect 301 http://old-subdomain.old-domain.com http://new-subdomain.new-domain.com
This will redirect any requests from http://old-subdomain.old-domain.com
to http://new-subdomain.new-domain.com
.
Redirect all URLs with a specific query string to a new URL
RedirectMatch 301 ^/(.*)\?old-query-string http://example.com/new-url?new-query-string
This will redirect any URLs that match the pattern /old-pattern?old-query-string
to http://example.com/new-url?new-query-string
.
Note:
301
is the HTTP status code for a permanent redirect. You can use302
for a temporary redirect if needed.- Make sure to update the
.htaccess
file in the root directory of your website. - If you're using a virtual host, you may need to update the
.htaccess
file in the virtual host's document root directory. - Be careful when updating
.htaccess
files, as incorrect syntax can cause issues with your website.
It's also important to note that you can use mod_rewrite
rules to achieve more complex redirects. For example:
RewriteEngine On
RewriteRule ^old-url$ http://example.com/new-url [R=301,L]
This will redirect any requests to /old-url
to http://example.com/new-url
.