Part of my URL structure changed can I rewrite part of a URL in htaccess instead of updating thousands of urls one-by-one by hand?
mywebsite . com /old/component/tag/colorado-sunday-meetings
to
mywebsite . com** /new/component**/tag/colorado-sunday-meetings
If it was this one url I could 301 it but it’s thousands of urls with that 1 change in the url structure.
I have been looking for examples online but I don’t know if any of these work and where I would modify if so.
…
Removing the Query String RewriteRule ^/url /url?
Adding to the Query String Keep the existing query string using the Query String Append flag, but add var=val to the end. RewriteRule ^/url /url?var=val [QSA]
Rewriting For Certain Query Strings Rewrite URLs like http://askapache.com/url1?var=val to http://askapache.com/url2?var=val but don’t rewrite if val isn’t present. RewriteCond %{QUERY_STRING} val RewriteRule ^/url1 /url2
Modifying the Query String Change any single instance of val in the query string to other_val when accessing /path. Note that %1 and %2 are back-references to the matched part of the regular expression in the previous RewriteCond. RewriteCond %{QUERY_STRING} ^(.)val(.)$ RewriteRule /path /path?%1other_val%2
…
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi there,
In your case, it’s possible to use the Apache mod_rewrite module in your .htaccess file to accomplish this. You don’t need to modify the query string - you just want to change a static part of the URL.
Here’s an example of how to do this:
RewriteEngine On
RewriteBase /
RewriteRule ^old/component/(.*)$ /new/component/$1 [R=301,L]
In this example:
RewriteEngine On turns on the rewrite engine so that it can start processing rewrite rules.RewriteBase / sets the base URL for per-directory rewrites.RewriteRule ^old/component/(.*)$ /new/component/$1 [R=301,L] is the rule that actually does the work. It matches any URL that starts with old/component/ and captures everything that comes after it. It then redirects to new/component/ followed by whatever it captured. The [R=301,L] part at the end specifies that it should send a 301 (permanent) redirect and that this is the last rule that should be processed.Before implementing this, please make sure to backup your .htaccess file and test on a non-production environment if possible. Also, remember that after making changes to .htaccess, you may need to clear your browser cache before testing as 301 redirects can be cached by your browser.
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.