Htaccess 301 regex question
-
I need some help with a regex for htaccess. I want to 301 redirect this:
to this:
changes:
- different protocol (http -> https)
- add 'www.'
- different domain (olddomain and newdomain are constants)
- different subdirectory (oldsubdir and newsubdir are constants)
- 'fruit' is a variable (which will contain only letters [a-zA-Z])
- is it possible to make 'fruit' UPPER case on the redirect (so 'fruit' -> 'FRUIT')
- remove '.aspx'
I think it's something like this (placed in the .htaccess file in the root directory of olddomain):
RedirectMatch 301 /oldsubdir/(.*).aspx https://www.newdomain.com/newsubdir/$1
Thanks.
-
Hi Mike,
I am happy it worked out.
you can use this to check as well
http://seo-website-designer.com/Response-Redirect-Header-Checker
All the best,
Tom
-
Hey Tom,
I guess this was mostly a regex question. I think I figured it out:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$
RewriteRule ^oldsubdir/([a-zA-Z.]+).aspx$ https://www.newdomain.com/newsubdir/$1 [R=301]Thanks for the tips.
-
I would use http://www.htaccessredirect.net/
this can help to
http://www.webconfs.com/how-to-redirect-a-webpage.php
Let me know if you need anything
-
Thanks, Tom. But 'fruit' was a placeholder for a variable page name that can contain letters or a '.' (sorry if that wasn't clear). for example, 'fruit' could be "abc.def.aspx" and I'd like it to become "ABC.DEF" (strip off the .aspx and uppercase it). Need a regex. The '.' within 'fruit' may or may not be present. But the page will always have the suffix '.aspx' that I want to strip off.
I'd also like to do it with an .htaccess statement instead of VBScript. Running on unix.
-
You would use
<%@ Language="VBScript" %>
<%
' ASP permanent URL redirect - generated by www.rapidtables.com
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "https://www.newdomain.com/newsubdir/FRUIT"
Response.End
%>try
http://www.rapidtables.com/web/tools/redirect-generator.htm
I hope this helps,
Tom
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
301 Redirects for Multiple Language Sites in htaccess File
Hi everyone, I have a site on a subdomain that has multiple languages set up at the domain level: https://mysite.site.com, https://mysite.site.fr , https://mysite.site.es , https://mysite.site.de , etc. We are migrating to a new subdomain and I am trying to create 301 redirects within the htaccess file, but I am a bit lost on how to do this as it seems you have to go from a relative url to an absolute - which would be fine if I was only doing this for the english site, but I'm not. It doesn't seem like I can go from absolute url to an absolute url - but I could be wrong. I am new to editing the htaccess file - so I could definitely use some advice here. Thanks.
Intermediate & Advanced SEO | | amberprata0 -
Is there any benefit to changing 303 redirects to 301?
A year ago I moved my marketplace website from http to https. I implemented some design changes at the same time, and saw a huge drop in traffic that we have not recovered from. I've been searching for reasons for the organic traffic decline and have noticed that the redirects from http to https URLs are 303 redirects. There's little information available about 303 redirects but most articles say they don't pass link juice. Is it worth changing them to 301 redirects now? Are there risks in making such a change a year later, and is it likely to have any benefits for rankings?
Intermediate & Advanced SEO | | MAdeit0 -
Question on Indexing, Hreflang tag, Canonical
Dear All, Have a question. We've a client (pharma), who has a prescription medicine approved only in the US, and has only one global site at .com which is accessed by all their target audience all over the world.
Intermediate & Advanced SEO | | jrohwer
For the rest of the US, we can create a replica of the home page (which actually features that drug), minus the existence of the medicine, and set IP filter so that non-US traffic see the duplicate of the home page. Question is, how best to tackle this semi-duplicate page. Possibly no-index won't do because that will block the site from the non-US geography. Hreflang won't work here possibly, because we are not dealing different languages, we are dealing same language (En) but different Geographies. Canonical might be the best way to go? Wanted to have an insight from the experts. Thanks,
Suparno (for Jeff)1 -
DA vs Relevancy - Trade Off Question
Hey Guys We all know that relevancy largely trumps DA nowadays. What I am wondering is if there is a DA 'level' at which relevancy doesn't really matter - you probably still want a backlink from that site... For example, sites with DA of 100 we probably want backlinks from. So where do you draw the line? What I mean is for a high DA 'non relevant' site, what DA is 'acceptable' where you start to disregard relevancy? I'm thinking something like 70 and above would like some other thoughts... Obviously you would still be building relevant links too, developing content to do so and all that good stuff. I am just wondering what DA I should focus on for building non-relevant links ALONGSIDE relevant links 🙂 Thanks
Intermediate & Advanced SEO | | GTAMP0 -
When is it time to kill 301 redirects
3 months we updated our site design design and as such lots of page urls changed. At the time we 301 redirected about 100 pages. (All pages are on the same domain - 301 redirects like .com/about-us/company to .com/company) Anyhow my question is should I leave these redirects active indefinitely or kill them assuming value has passed through by now? Your Thoughts are welcomed. Thanks, Glen.
Intermediate & Advanced SEO | | AdvanceSystems0 -
A question of rankings (with actual domains)
Working with the main site featured in this Open Site Explorer comparison (you'll need a pro account to view this), and have been for quite some time. Recently we've slid behind Ebay (huge brand, I get it), but the other competitors don't really make sense to me. Main phrase is pontoon boats, and maybe I'm too close to this, but we seem to be in the best shape overall in terms of the domain, the page itself, and even our social media is pretty successful (we're closing in on 5,000 likes and have a pretty engaged audience). More internal linking is an opportunity, but I'd like another set of eyes (or several for that matter) to weigh in on opinions. I'm a bit stumped. Thanks Mozzers!
Intermediate & Advanced SEO | | NetvantageMarketing0 -
Modify .htaccess
Hi everybody, I need to modify the .htaccess in order to include a 301 redirect. But I am having some problems with this. I downloaded the file into my computer and then modified it with windows notepad, but when I upload it again to the server it is not working and turns down my website. Even if I do not change anything on it. So I guess there is a problem in the saving process. Any Idea or suggestion? Thanks, G.
Intermediate & Advanced SEO | | SilbertAd0 -
301 or What?
Can someone please tell me the 100% correct way to set this up. Would I be right to set up a site this way... type in the browser..."example.com" and it re directs to "www.example.com. or, if i type in "example.com" it goes to example.com. or if I type in "www.example.com" it goes to "example.com"? or, type in "www.example.com" and it goes to "example.com" wouldn't most site link to a "www" version? PS whay isn;t the correct way set up by our HOST.? They should know what is most beneficial? Thanks
Intermediate & Advanced SEO | | SEObleu.com0