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
-
Questions About Link Detox
Greetings: In April of 2014 an SEO firm ran a link removal campaign (identified spammy links and uploaded a disavow). The overall campaign was ineffective and MOZ domain rank has fallen to 24 from about 30 in the last year and traffic is 20% lower. I purchased a basic package for Link Detox and ran a report today (see enclosed) to see if toxic links could be contributing to our mediocre rankings. As a novice I have a few questions for you regarding this the use of Link Detox: -We scored a domain wide detox risk of 1,723. The site has referring root domains with 7113 links to our site. 121 links were classified as high audit priority. 56 as medium audit priority. 221 links were previously disavowed and we uploaded a spreadsheet containing the names of the previously disavowed links. We had LinkDetox include an analysis of no-follow links as they recommend this. Is our score really bad? If we remove the questionable links should we see some benefit in ranking? -Some of the links we disavowed last year are still linking to our site. Is it worthwhile to include those links again in our new disavow file? -Prior to filing a disavow we will request that Webmaster remove offending links. LinkDetox offers a package called Superhero for $469.00 that automates the process. Does this package effectively help with the entire process of writing and tracking the removal requests? Do you know of any other good alternatives? -A feature called "Boost" is included in the LinkDetox Super Hero package. It is suppose to expedite Google's processing of the disavow file. I was told by the staff at Link Detox that with Boost Google will process the disavow within a week. Do you have any idea if this claim is valid??? It would be great if it were true. -We never experienced any manual penalty from Google. Will uploading a disavow help us under the circumstances? Thanks for your feedback, I really appreciate it!!! Alan p2S6H7l
Intermediate & Advanced SEO | | Kingalan10 -
A few important mobile SEO questions
I have a few basic questions about mobile SEO. I'd appreciate if any of you fabulous Mozzers can enlighten me. Our site has a parallel mobile site with the same urls, using an m. domain for mobile and www. for desktop. On mobile pages, we have a rel="canonical" tag pointing to the matching desktop URL and on desktop pages we have a rel="alternate" tag pointing to the matching mobile URL. When someone visits a www. page using a mobile device, we 301 them to the mobile version. Questions: 1. Do I want my mobile pages to be indexed by Google? From Tom's (very helpful) answers here, it seems that I only want Google indexing the full site pages and if the mobile pages are indexed it's actually a duplicate content issue. This is really confusing to me since Google knows that it's not duplicate content based on the canonical tag. But - he makes a good point - what is the value of having the mobile page indexed if the same page on desktop is indexed (I know that Google is indexing both because I see them in search results. When I search on mobile Google serves the mobile page and when I search on desktop Google serves me the desktop page.)? Are these pages competing with each other? Currently, we are doing everything we can do ensure that our mobile pages are crawled (deeply) and indexed, but now I'm not sure what the value of this is? Please share your knowledge. 2. Is a mobile page's ranking affected by social shares of the desktop version of the same page? Currently, when someone uses the share buttons on our mobile site, we share the desktop url (www. - not m.). The reason we do this is that we are afraid that if people are sharing our content with 2 different url's (m.mysite.com/some_post and www.mysite.com/some_post) the share count will not be aggregated for both url's. What I'm wondering is: will this have a negative effect on mobile SEO, since it will seem to Google that our mobile pages have no shares, or is this not a problem, since the desktop pages have a rel="alternate" tag pointing to mobile pages, so Google gives the same ranking to the mobile page as the desktop page (which IS being shared)?
Intermediate & Advanced SEO | | YairSpolter0 -
Htaccess redirect veriables
Hey, I'm trying to redirect all instances of "/archive_details.php?id=*" to "/public-affairs-job-archive.php". Is the below code correct? Redirect 301 /archive_details.php?id=* /public-affairs-job-archive.php Thanks, Luke.
Intermediate & Advanced SEO | | NoisyLittleMonkey0 -
Should I run 302 first before implementing 301?
I just want to ask if it is necessary to run 302 redirections first before redirecting old to new URLs permanently. I heard that we should run temporary redirects first so we can check after and to avoid passing the link juice but I want to hear thoughts from experts. Do i need to test 302s for old pages that are still live or should we redirect old URLs once these pages already removed from the site?
Intermediate & Advanced SEO | | esiow20130 -
Mass 301 redirect from a sub-domain - using Joomla or htaccess
How is best to mass redirect old domains - Listing the URL's in htaccess? We are looking to use Joomla as a CMS - transferring a blog from a sub-domain to the main site and want to 301 all the sub domain blog posts - any ideas?
Intermediate & Advanced SEO | | JohnW-UK0 -
How long for a 301 redirect to pass PR?
Hi, How long does it take for a 301 redirect to pass PR/Juice to the new domain it's redirecting to? From what I understand you tell Google in Webmaster tools this domain is now going to this domain and then setup a file on the old domains hosting to redirect to the new. And that's it! If that is correct how long does it take? 2 days, 2 weeks, months, maybe never??? Cheers
Intermediate & Advanced SEO | | activitysuper0 -
Site Structure Question
Hi All, Got a question about site structure, I currently have a website where everything is hosted on the root of the domain. See example below: site.com/men site.com/men-shorts site.com/men-shorts-[product name] I want to change the structure to site.com/men/shorts/[product-name] I have asked a couple of SEOs and some agree with me that the structure needs to be changed and some say that as long as I dictate the structure with internal links and breadcrumbs the URL structure doesn't matter... What do you guys think? Many thanks, Carlos
Intermediate & Advanced SEO | | Carlos-R0 -
Canonical, 301 or code a workaround?
Hi, Recently I've been trying to tackle an issue on one of my websites. I have a site with around 400 products and 550 pages total. I've been pruning some weaker pages and pages with shallow content, and it's been working really well. My current issue is this: There are about 20 store brands of 6 products on my site that each have their own page. They are identical products just re-branded. Writing content for each of these pages has been difficult, as it's a fairly dry product too. So I have around 120 pages of dry content that is unique but not much different from one another. I want to consolidate but I am not sure how yet. Here is what I am thinking: 1. 301 - I pick one product page as the master, 301 all the other duplicate products to it and then make one page of great content that encompasses all of them. If the 301 juice gets diluted over time I might miss out on some long tails, but I could also gain a lot more from a great content page with 500+ words of really good content as opposed to pages with 150-250 words of just so so content. 2. Canonical - Similar to above. I pick a master page and canonical the other pages to it. Then I could use the great content on all the pages, and still have pages for the specific products. The pages might not show up in search engines but would still be searchable on my site. 3. Coded solution - In my CMS I could always make a workaround where the products still appear on the brands page (just their name with a link to the product page) but all the links direct to a master page. I realize all the solutions are fairly similar, although I am not sure which is ideal. Option 3 is the most expensive/time consuming but it would drop my page total down to around 450 pages. For a while now (dating back to before Panda) I've been trying to get rid of the low quality and outdated product pages so I could focus on the more popular and active pages. Dropping my page total would also help in the SEO efforts as the sheer volume of pages that need links right now is high, and obviously the less pages I have the more time I can spend on each page (content and link building). So what do you think? Should I do any of the 3, a combination of the 3 or something different? Cheers, Vinnie
Intermediate & Advanced SEO | | vforvinnie0