Rewrite and Redirect with htaccess - .htaccess

A client has asked us to replicate all the content from an old domain (bcsbd.com) to their main domain (ywcacam.org), and also create redirects so the old URLs are still functional. Unfortunately, the URLs aren't exact matches, e.g., [olddomain]/about has become [newdomain]/about_soo_bahk_do. There are less than 10 specific URLs to handle, which we initially did successfully using Redirect statements in the old domain's htaccess file:
# redirect specific pages to page on new domain
Redirect /about http://www.ywcacam.org/about_soo_bahk_do
We also need a catch-all, so that any other requests go to a specific URL on the new domain, e.g., www.bcsbd.com/somefile becomes www.ywcacam.org/soo_bahk_do. We handled this using Rewrite statements:
# catch-all for any requests not specified above
RewriteCond %{HTTP_HOST} ^(bcsbd.com|www.bcsbd.com) [NC]
RewriteRule ^(.*)$ http://www.ywcacam.org/soo_bahk_do [L]
Quick research showed the Rewrite directives (using mod_rewrite) would always be processed before the Redirect directives (using mod_alias). So we replaced the Redirects with Rewrites:
Options +FollowSymlinks
RewriteEngine On
RewriteRule /about http://www.ywcacam.org/about_soo_bahk_do [L]
RewriteRule /programs http://www.ywcacam.org/programs_soo_bahk_do [L]
...
# catch-all for any requests not specified above
RewriteCond %{HTTP_HOST} ^(bcsbd.com|www.bcsbd.com) [NC]
RewriteRule ^(.*)$ http://www.ywcacam.org/soo_bahk_do [L]
The problem is that just the catch-all is working - the new Rewrite rules are being ignored. What are we doing wrong in those statements?
Thanks in advance for the help!

Related

Problem with using Rewriterule in htaccess

With RewriteRule I've always cleaned my URLs as following:
RewriteRule ^page/(.*)$ page.php?urlkey=$1 [QSA]
My new host doesn't allow me to use Options +FollowSymLinks and therefore I cannot use the / anymore. So I've changed my RewriteRule to:
RewriteRule ^page-(.*)$ page.php?urlkey=$1 [QSA]
However, I need to redirect all my former URLs to the new version. I tried doing this using the following rule:
RewriteRule ^page/(.*)$ page-(.*)$ [R=301,L]
This is however not working. I've also tried to just make a Redirect in my .htaccess:
Redirect 301 https://www.example.com/page/urlkey https://www.example.com/page-urlkey
This is also not working.
EDIT
As requested the actual code below:
RewriteEngine on
RewriteRule ^citywalk-(.*)$ citywalk.php?urlkey=$1 [QSA]
RewriteRule ^citywalk/(.*)$ citywalk-(.*)$ [R=301,L]
For example the citywalk The Historical Centre has a urlkey the-historical-centre. The old url is citywalk/the-historical-centre.
To test this specific case and other technique:
Redirect 301 /citywalk/the-historical-centre https://example.com/citywalk-the-historical-centre
By visiting https://example.com/citywalk/the-historical-centre no redirecting takes place (the url stays the same in the browser) and no urlkey is found.

How can I redirect match full subdomain urls, but make the root subdomain go elsewhere using htaccess

I have a bunch of old urls from a subdomain I need redirected to another subdomain and retain the url pattern. The issue I'm struggling with is getting just the root subdomain to redirect to another location.
For context, here is an example of the old url structure:
https://oldsub.maindomain.com/2015/07/30/url-title/
Which I need redirected to:
https://blog.maindomain.com/blog/2015/07/30/url-title/
Notice it needs to be directed to a new blog subdomain and there is a blog slug added after the main domain.
If anyone visits just the root of the old subdomain (https://oldsub.maindomain.com/) I need that redirected to a URL structure on just the main domain (https://maindomain.com/specific-url-title/)
I have my .htaccess within the root of the subdomain directory.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsub\. [NC]
RewriteCond %{HTTP_HOST} ^(?:oldsub\.)?(.+)$ [NC]
RewriteRule ^ https://blog.%1%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{HTTP_HOST} ^oldsub\.maindomain\.com$ [NC]
RewriteRule ^ https://maindomain.com/specific-url-title/ [L,NE,R=301]
In the .htaccess of the root domain I have:
RewriteCond %{HTTP_HOST}
RedirectMatch 301 ^/blog/(.*)$ https://blog.maindomain.com/blog/$1
So the old blog posts redirect properly to the blog.maindomain.com/blog/rest-of-url but the last two lines aren't working as the old subdomain request simply goes to blog.maindomain.com/blog as well.
The file structure is:
/public_html (maindomain.com)
.htaccess
/oldsub.maindomain.com (oldsub.maindomain.com)
.htaccess
blog.maindomain.com lives on another server (on hubspot platform)
Your current redirects are not correct, so these need to be fixed. (Although you should have seen a redirect of some kind.)
RewriteCond %{HTTP_HOST} ^oldsub\. [NC]
RewriteCond %{HTTP_HOST} ^(?:oldsub\.)?(.+)$ [NC]
RewriteRule ^ https://blog.%1%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{HTTP_HOST} ^oldsub\.gppcpa\.com$ [NC]
RewriteRule ^ https://maindomain.com/specific-url-title/ [L,NE,R=301]
The problems with these directives:
The first rule does not redirect to the /blog subdirectory.
These rules conflict. The first rule will also redirect the document root of the subdomain. Consequently, the second rule will never be processed.
Since the subdomain in question is completely outside of your maindomain, in terms of where these hostnames point to on your filesystem, then there is no need to check the hostname in these directives since only the oldsub subdomain can access this area.
Try the following instead:
RewriteRule . https://blog.maindomain.com/blog%{REQUEST_URI} [R=302,L]
RewriteRule ^ https://maindomain.com/specific-url-title/ [R=302,L]
The single dot in the first RewriteRule pattern matches "something" (not "nothing"). When the URL-path is empty (ie. the document root) this falls through to the second directive.
Make sure you clear your browser cache before testing.
Test with 302 (temporary) redirects and only change to 301 (permanent) when you are sure it's working OK. This is to avoid any caching issues, since 301s are cached persistently by the browser (including those made in error).
RewriteCond %{HTTP_HOST}
RedirectMatch 301 ^/blog/(.*)$ https://blog.maindomain.com/blog/$1
From your maindomain .htaccess file... That RewriteCond directive is erroneous here and should be removed.
To avoid potential conflicts with mod_rewrite you should change the mod_alias RedirectMatch directive to mod_rewrite. For example:
RewriteEngine On
RewriteRule ^blog/(.*) https://blog.maindomain.com/blog/$1 [R=302,L]
Different Apache modules (mod_rewrite, mod_alias, etc) run at different times throughout the request and run independently of each other. mod_rewrite usually runs first (regardless of the apparent order of the directives in your config file), but this also means that mod_alias (Redirect, RedirectMatch) will always run, even though an earlier mod_rewrite directive (RewriteRule) might have rewritten the URL and stopped execution.

How to Re-Direct non WWW, non HTTPS to WWW, HTTPS without Multiple Re-Directs?

I hope you can help with this htaccess issue please? I basically have the htaccess rules working apart from in one scenario. This is the case where a user visits the site on a non HTTPS and non WWW links.
Re-Directs
User visits on http/non-WWW URL
User is redirected initially to the http/WWW URL
User is then redirected to https://www.website
You can see the behaviour here:
http://childrens-curtains.co.uk
All other scenarios work fine.
I want to try to remove the 2nd redirect from the sequence so it behaves like this:
User visits on http and is immediately redirected to the https/www version without the 2nd step (if that makes sense?)
The current scenario causes multiple re-directs, which I understand is a bad approach from an SEO perspective.
This is my htaccess redirect rule:
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [L,R=301]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context
You can try this code keepin into .htaccess file.
To add www add following code.
RewriteCond %{HTTP_HOST} ^**domainname**\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$
RewriteRule ^index\.html$ "http\:\/\/www\.domainname\.com\/" [R=301,L]
To add http add following code.
RewriteCond %{HTTP_HOST} ^domainname.com [NC]
RewriteRule ^(.*)$ http://www.domainname.com/$1 [L,R=301]
Which will add both http as well as www to URL.
Thanks.

Mod Rewrite redirect URL with query string to pretty url

I successfully changed my URLs from ugly ones with several parameters in the querystring to clean looking ones with the help of mod rewrite. However, there are many url's for my site. Rather than go back and edit the href attribute on each and every one of my anchor tags, I tried to write a redirect function in the .htaccess file that automatically redirects the old url to the new one.
In my .htaccess file, I have the following:
RewriteEngine On
Redirect teams.php?league=$1&team=$2&year=$3&tab=$4 teams/(.*)/(.*)/(.*)/(.*)
RewriteCond %{REQUEST_URI} !^(css|js|img)/
RewriteRule ^teams/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ teams.php?league=$1&team=$2&year=$3&tab=$4 [L]
No luck though... any thoughts?
Thanks
You need to do a check that the old URL with the php in it is actually being requested by matching against %{THE_REQUEST}, otherwise it'll redirect loop forever (e.g. user goes to team.php, serve redirects to teams, browser requests teams, server rewrites as teams.php, server sees "teams.php" and redirects to teams, browser requests teams, server rewrites as teams.php, etc. etc.)
RewriteEngine On
# redirect when the user actually requests for teams.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /teams\.php\?league=([^&]+)&team=([^&]+)&year=([^&]+)&tab=([^\ ]+)
RewriteRule ^teams\.php$ /teams/%1/%2/%3/%4? [R=301,L]
# internally rewrite any /teams/ URI
RewriteCond %{REQUEST_URI} !^(css|js|img)/
RewriteRule ^teams/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ teams.php?league=$1&team=$2&year=$3&tab=$4 [L]

htaccess redirects from URLs on one domain to the root of another domain

Currently what is happening is people are accessing old URLs from google like icpaweb.com/site/pages/about-us/ and being sent to their corresponding urls on icpaweb.org : icpaweb.org/site/pages/about-us.
What I want is to send people from: icpaweb.com/site/pages/about-us to icpaweb.org/ without any of the succeeding url segments.
How do I do this?
If you have to use an .htaccess file, you can use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} icpaweb.com$ [NC]
RewriteRule .* http://icpaweb.org/ [R=301,L]
That will 301 redirect all requests for icpaweb.com to the index root of icpaweb.org. If you don't want 301, it can just be R.
You'll need to replace or turn off whatever mechanism is doing your redirecting now, they may not be compatible.
Use an url rewrite rule.
2 steps:
Write a RewriteCond so that the following rewrite rule only apply for url with host being icpaweb.com like RewriteCond %{HTTP_HOST} icpaweb.com$ [NC] The [NC] is for case insensitive match
Write a rewrite rule that convert all input to what you want like RewriteRule ^.*$ http://icpaweb.org/ [L]The [L] is to stop the rewriting to this rule if rule executed.

Resources