.htaccess Rewrite Based on Existence of Path in URL - .htaccess

Here's the scenario, I have a website that used to be a static HTML site and WordPress blog using a subdomain (http://blog.domain.com).
I recently combined everything into a single WordPress installation. To maintain old links I had to rewrite requests like "http://blog.domain.com/index.php/2010/10/16/post-name" to "http://domain.com/index.php/2010/10/16/post-name". My problem is that when trying to visit just "http://blog.domain.com", I get redirected to "http://domain.com" when I want it to go to "http://domain.com/index.php/blog".
So, if a user requests "http://blog.domain.com" (by itself, with or without slash), I want it to go to "http://domain.com/index.php/blog". If they request an old URL of "http://blog.domain.com/some-link-to-a-post", I want it to redirect to "http://domain.com/some-link-to-a-post". In other words, if it's a URL to an actual post, I just want to strip the "blog" subdomain. If it's the old link to the main blog page, I want to remove the "blog" subdomain and append "/index.php/blog"
http://blog.domain.com/ -> http://domain.com/index.php/blog
http://blog.domain.com/index.php/2010/10/16/post-title -> http://domain.com/index.php/2010/10/16/post-title
Hopefully that's clear. I'm not an htaccess expert, so hopefully someone can help me out here. Thanks in advance!

Using the [L] command at the end of a rewrite will tell htaccess that this is the last rule it should match. If you put a rule to match your first condition at the top and the other rewrite rule you said you had already created after it, you should get your expected result.
Try this:
RewriteRule ^blog.domain.com(/?)$ domain.com/index.php/blog [L]
# Your other rewrite here #

I couldn't get that solution to work. However, I used the following:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/index.php/blog/$1 [R=301,L]
That ends up in a URL like http://domain.com/index.php/blog/index.php/2010/06/04/post-title, but Wordpress is smart enough to fix it.

Related

htaccess rewrite url by matching only the first part of the url

I've been struggling to find a solution to this very simple problem. You would think that this is the classic "please help me I need to redirect this url to newurl" type of thing but, believe me, it isn't that easy.
I have a domain example.com, this example.com has some language variants:
example.com/mx
example.com/us
example.com/ca
example.com/cl
example.com/xx
and so on, example
.com is the index to choose between countries and it exists too but has no content.
We recently moved our example.com/mx to a whole new domain (with a different system) example.com.mx so we proceeded to permanently redirect every url there to the its new url in the example.com.mx
While we were at it, we discovered that our multilingual wordpress installation on example.com was duplicating all website's content in the root file, something which shouldn't have happened.
So, we have like
example.com/us/folder
example.com/cl/folder
example.com/ca/folder
etc
AND
example.com/folder which shouldn't exist
There are tons of directories that are duplicated there, we already know where to redirect each of these directories but we just can't find the right line of code to do it.
For example, we have set up this htaccess rule to deal with all the /mx/ folder, which works well:
RewriteRule ^mx/shop/ https://www.example.com.mx/? [L,R=301]
RewriteRule ^mx/home/ https://www.example.com.mx/? [L,R=301]
RewriteRule ^mx/page/ https://www.example.com.mx/? [L,R=301]
RewriteRule ^mx/tag/ https://www.example.com.mx/? [L,R=301]
But when trying to extend this same logic to the "/shop/" directory, it matches the same directory in EVERY country folder, something we obviously don't want.
This is what we tried, described previously:
RewriteRule ^/tag/ https://www.example.com.mx/newtagurl? [L,R=301]
This obviously also redirects example.com/ca/tag/, because the ^.
So my question is, how to properly redirect to these cases ONLY matching the beginning https://example.com/tag/ string instead of matching every /tag/ case in every country folder?
Thanks!

Redirect 301 www.example.com/directory/tour1 to www.example.com/tour1

Hi I have a situation with the mytic HTACCESS file. Im trying to fix a broken URL.
this folder never existed was a permalink created in WP.I need to redirect 301 all this broken links to avoid problems with Google.
The URL used to be like this
www.example.com/directory/tour1, www.example.com/directory/tour2, www.example.com/directory/tour3 and so on.
Now the url has changed so all the tours are under the root
www.example.com/tour1, www.example.com/tour2...
I need to make that all the queries to www.example.com/directory/WHATEVER
to point to www.example.com/WHATEVER
Thanks for helping me understand this universe of redirections... \
i HAVE TRIED alot of codes, none of them does the job.
RewriteRule ^tulum-tours/(.*) http://www.aguaclaraproject.com/
In your .htaccess in your www-root use the following rule:
RewriteEngine on
RewriteRule ^tulum-tours/(.*)$ /$1 [R,L]
Change the R flag to R=301 after testing it works correctly. Read the documentation for more information.

forcibly redirect to correct folder

I am new in htaccess.
I updated some SEO pages in my live site after one day some Url changes came so i changed the url again. but google already indexed it. So i want if some one found old url it will redirect to new url But in case of SEO pages only not for other pages.It means it wont affect to any other place.and there are not one page(it is 40-50 pages) can anybody give answer through htaccess or cakephp.
Old Url-
www.testenergy.com/test-energy-reviews
new url-
www.testenergy.com/s/test-energy-reviews
And there are also four senario-
www.testenergy.com/test-energy-reviews
www.testenergy.com/Test-Energy-Reviews
www.testenergy.com/s/test-energy-reviews
www.testenergy.com/s/Test-Energy-Reviews
All these four links will redirect to www.testenergy.com/s/test-energy-reviews Url only
Assuming you have mod_rewrite rules somewhere, you probably want to stick to mod_rewrite. You'll need to add these to the htaccess file in your document root, preferably above any other rules that are there:
RewriteEngine On
RewriteRule ^/?test-energy-reviews$ /s/test-energy-reviews [L,NC,R=301]
RewriteRule ^/?s/Test-Energy-Reviews$ /s/test-energy-reviews [L,R=301]
The NC flag ignores case, so it covers both /test-energy-reviews and /Test-Energy-Reviews. The second rule takes care of /s/Test-Energy-Reviews
I'm not sure why /s/test-energy-reviews (3rd one) is one of your scenarios, since it is exactly what you want to redirect to.
Try This ..!!
Router::redirect('/test-energy-reviews', 'http://www.testenergy.energy/s/test-energy-reviews');
write this line in Controller.
/********* Redirect Url fo small letter if some one type in uppercase in url bar****/
preg_match( '/[A-Z]+/',$this->params->url, $upper_case_found );
if(count($upper_case_found)) {
// Now redirect to lower case version of url
header("HTTP/1.1 301 Moved Permanently");
header("Location: " . ROOTPATH.strtolower($this->params->url) );die();
}
/**** End Code******/
OR in htaccess write following code:
RewriteEngine On
RewriteRule ^/?test-energy-reviews$ /s/test-energy-reviews [L,NC,R=301]

htaccess rule to redirect?

I have a link like www.example.com/hello-1/hey-2, now what I want to do is that I want to redirect to this link from www.example.com/hello-1/hey. How to write a htaccess rule for this. I have tried searching on it, tried the same way to reduce index.php but that didn't work.Thank you.
See, basically I want people to be redirected when the type www.example.com/hello-1/hey this was my old link. I want them to be redirected to www.example.com/hello-1/hey-2, where this 2 is important as it's passing a parameter for me.
Basically, it's possible to do so;
RewriteEngine On
RewriteRule ^hello-1/hey$ hello-1/hey-2 [R=301,L]

Htaccess Rewrite to remove directory

I need help creating a rewrite string to remove the ~12345678/ from the following: www.example.com/~12345678/file.
This doesn't appear on every link. What happened is during development links were created using the development url, there are a couple dozen links that appear this way. The rest of the links are fine. I already use a rewrite to remove index.php.
Try this (this will redirect any request for www.example.com/~12345678/dir/file.ext to www.example.com/dir/file.ext) :
RewriteRule ^\~[a-zA-Z0-9]+/(.*)?$ /$1 [L,QSA,R=301]

Resources