htaccess redirect rules - How to redirect up one level? - .htaccess

http://mysite.com/level-1/level-2/level-3/
I want to redirect to
http://mysite.com/level-1/level-2/
"level-1" and "level-2" can be anything the user enters... (not these exact words)
Could you direct me to a tutorial or give me a few pointers?
Thanks a lot!!

Based on your comment (from level-3 to level-2 folder EXACTLY with 301 Permanent Redirect):
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+/[^/]+/)[^/]+/$ http://www.example.com/$1 [QSA,R=301,L]
This rule will redirect example.com/hello/pink/kitten/ to example.com/hello/pink/
If URL structure is different, then NO redirect will occur:
missing trailing slash (e.g. example.com/hello/pink/kitten)
4-level deep URL (e.g. example.com/hello/pink/kitten/family/)
This rule needs to be placed in .htaccess in website root folder. If placed elsewhere (e.g. Apache config file, inside <VirtualHost>, for example) the rule needs small tweaking.

You just want to throw away what's after the second slash. This is an easy rule:
RewriteRule ^(.*?/.*?/).+$ $1
Then investigate whether you want things like [L,QSA]. You may or may not.

Related

Htaccess and redirect all content of folder, but exlude the main folder

I got the following urls:
domain.com/categoryA/articleA
domain.com/categoryA/articleB
I want to redirect:
domain.com/categoryA/articleA -> domain.com/categoryB/articleA
domain.com/categoryA/articleB -> domain.com/categoryB/articleB
but leave it as it is and do not redirect the main folder: domain.com/categoryA/
I tried to use the rule:
RewriteRule ^categoryA/(.*)$ /categoryB/$1 [R=301,NC,L]
but it also redirect domain.com/categoryA/ to domain.com/categoryB/
How to exclude from the above rewrite rule the redirection of the main folder (categoryA), but still redirect all that is in the folder (and then change also the root folder)?
I am looking for a solution that is SEO friendly (I got the same articles in two categories, but want still to have indexed domain.com/categoryA, but the rest only as domain.com/categoryB/xxx.
Best Greetings,
Mat
With your shown samples/attempts, please try following Rules in your .htaccess file. Please make sure to place this rule under your domain redirect rule(if its there), also make sure to clear your browser cache before testing your URLs.
RewriteRule ^categoryA/([\w-]+)/?$ /categoryB/$1 [R=301,NC,L]

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]

Stop mod_rewrite returning REQUEST_URI when (.*) is empty

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^mocks/site/(.*)$ http://thelivewebsite.com/$1 [R=301,L]
That is my htaccess file's contents.
The htaccess file is in the root directory of the hosting account and I just want to redirect the directory mocks/site/ to the new domain (with or without any extra directories).
eg: if someone goes to http://mywebsite.com/mocks/site then it needs to redirect to http://thelivewebsite.com. If they go to http://mywebsite.com/mocks/site/another/directory then it needs to redirect to http://thelivewebsite.com/another/directory. I hope that makes sense.
So the problem I have is that the htaccess code above seems to work pretty well when there is something after mocks/site/ however when there isn't something after that then the $1 in the redirect seems to reference the whole REQUEST_URI (eg: mocks/site/ rather than nothing - as there is nothing after it).
I don't know how to stop this. I thought about using a RewriteCond, but I'm not sure what to use there. I can't find anything that helps me to determine if there is anything after mocks/site/ or not.
Any help will be much appreciated.
Thank you.
That's very strange behaviour -- never seen anything like that. Therefore I think it could be something else (another rule somewhere -- on old or even new site). I recommend enabling rewrite debugging (RewriteLogLevel 9) and check the rewrite log (that's if you can edit Apache's config file / virtual host definition).
In any case, try this combination:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^mocks/site/$ http://thelivewebsite.com/ [R=301,L]
RewriteRule ^mocks/site/(.+)$ http://thelivewebsite.com/$1 [R=301,L]
It will do matching/redirecting in 2 steps: first rule is for exact directory match (so no $1 involved at all) and 2nd will work if there is at least 1 character after the /mocks/site/.
Alternatively (Apache docs even recommending this one) use Redirect directive (no need for mod_rewrite at all for such simple redirects):
Redirect 301 /mocks/site/ http://thelivewebsite.com/

.htaccess Rewrite Based on Existence of Path in URL

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.

How do I use .htaccess to redirect to a URL containing HTTP_HOST?

Problem
I need to redirect some short convenience URLs to longer actual URLs. The site in question uses a set of subdomains to identify a set of development or live versions.
I would like the URL to which certain requests are redirected to include the HTTP_HOST such that I don't have to create a custom .htaccess file for each host.
Host-specific Example (snipped from .htaccess file)
Redirect /terms http://support.dev01.example.com/articles/terms/
This example works fine for the development version running at dev01.example.com. If I use the same line in the main .htaccess file for the development version running under dev02.example.com I'd end up being redirected to the wrong place.
Ideal rule (not sure of the correct syntax)
Redirect /terms http://support.{HTTP_HOST}/articles/terms/
This rule does not work and merely serves as an example of what I'd like to achieve. I could then use the exact same rule under many different hosts and get the correct result.
Answers?
Can this be done with mod_alias or does it require the more complex mod_rewrite?
How can this be achieved using mod_alias or mod_rewrite? I'd prefer a mod_alias solution if possible.
Clarifications
I'm not staying on the same server. I'd like:
http://example.com/terms/ -> http://support.example.com/articles/terms/
https://secure.example.com/terms/ -> http://support.example.com/articles/terms/
http://dev.example.com/terms/ -> http://support.dev.example.com/articles/terms/
https://secure.dev.example.com/terms/ -> http://support.dev.example.com/articles/terms/
I'd like to be able to use the same rule in the .htaccess file on both example.com and dev.example.com. In this situation I'd need to be able to refer to the HTTP_HOST as a variable rather than specifying it literally in the URL to which requests are redirected.
I'll investigate the HTTP_HOST parameter as suggested but was hoping for a working example.
It's strange that nobody has done the actual working answer (lol):
RewriteCond %{HTTP_HOST} support\.(([^\.]+))\.example\.com
RewriteRule ^/terms http://support.%1/article/terms [NC,QSA,R]
To help you doing the job faster, my favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
You use this tool when you want to check the URL and see if they're valid or not.
I think you'll want to capture the HTTP_HOST value and then use that in the rewrite rule:
RewriteCond %{HTTP_HOST} (.*)
RewriteRule ^/terms http://support.%1/article/terms [NC,R=302]
If I understand your question right, you want a 301 redirect (tell browser to go to other URL).
If my solution is not the correct one for you, try this tool: http://www.htaccessredirect.net/index.php and figure out what works for you.
//301 Redirect Entire Directory
RedirectMatch 301 /terms(.*) /articles/terms/$1
//Change default directory page
DirectoryIndex
According to this cheatsheet ( http://www.addedbytes.com/download/mod_rewrite-cheat-sheet-v2/png/ ) this should work
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1
Note that i don't have a way to test this so this should be taken as a pointer in the right direction as opposed to an explicit answer.
If you are staying on the same server then putting this in your .htaccess will work regardless of the server:
RedirectMatch 301 ^/terms$ /articles/terms/
Produces:
http://example.com/terms -> http://example.com/articles/terms
or:
http://test.example.com/terms -> http://test.example.com/articles/terms
Obviously you'll need to adjust the REGEX matching and the like to make sure it copes with what you are going to throw at it. Same goes for the 301, you might want a 302 if you don't want browsers to cache the redirect.
If you want:
http://example.com/terms -> http://server02.example.com/articles/terms
Then you'll need to use the HTTP_HOST parameter.
You don't need to include this information. Just provide a URI relative to the root.
Redirect temp /terms /articles/terms/
This is explained in the mod_alias documentation:
The new URL should be an absolute URL beginning with a scheme and hostname, but a URL-path beginning with a slash may also be used, in which case the scheme and hostname of the current server will be added.
It sounds like what you really need is just an alias?
Alias /terms /www/public/articles/terms/

Resources