htaccess rewrite rule assistance - .htaccess

I need to create a rewrite rule for an htaccess file which will rewrite URL's such as:
http://archive.citylaw.org/bsa/2014/03.24.14/300-13-A.pdf
to
http://archive.citylaw.org/wp-content/uploads/sites/24/bsa/2014/03.24.14/300-13-A.pdf
The rule needs to match any link such as:
http://archive.citylaw.org/bsa/<file-path>
which is a link to a PDF file to the new location
http://archive.citylaw.org/wp-content/uploads/sites/24/bsa/<file-path>

Something like:
RewriteRule ^(bsa\/.*) /wp-content/uploads/sites/24/$1 [R]
The [R] tell apache to send a redirect, so the browser will update its navbar, usually this avoid browser not dealing well with non html files on rewrite.
Updated after Prix comments, I did forgot about .htaccess specificity
Second edit

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]

Redirect static html URL to an .htaccess rewritten URL

I have fully static website, now I want to change this website to dynamic. But problem is that static website having good traffic and I do not want to lose that traffic. For the dynamic website I already rewrite the URL using htaccess for SEO reasons. I want to redirect the static url to the rewritten url (which was written by my .htaccess).
(A) Static URL :
www.website.com/examples/java/datatype/boolean/printbooleanvalue.html
(B) Original Dynamic URL:
www.website.com/examples.php?language=?java&category=data-type&subcategory=boolean&exampleurl=print-boolean-value
(C) Rewritted Dynamic URL :
www.website.com/examples/java/data-types/boolean/print-boolean-value
So I want to redirect URL(A) to URL(C). Is there any way to do this ?
You can hardcode the mod rewrite to load the php file when the html file is called.
The SEO friendly url that you have created in your .htaccess is redundant. Instead of the SEO friendly URL, use the same url as your original site.
For example in your .htaccess file write the rule like
RewriteEngine On
RewriteBase /
RewriteRule examples/java/datatype/boolean/printbooleanvalue.html$ /examples.php?language=?java&category=data-type&subcategory=boolean&exampleurl=print-boolean-value [L]
This will load the php file whenever the original html file is requested and it will use the same url as the older one.
Hope that helps.
Your .htaccess code:
Redirect /examples/java/datatype/boolean/printbooleanvalue.html http://www.website.com/examples/java/data-types/boolean/print-boolean-value
In any case you run into the need to redirect:
Redirect /olddirectory/oldfile.html http://yoursite.com/newdirectory/newfile.html

Simple and neat .htaccess redirect help required

This is a strange one...
A while back I managed to write a .htaccess redirect that worked so that the URL was read like: www.website.com/mt?page=index - and what the real URL of this page was www.website.com/PageParser.php?file=index.php
The problem has been that the FTP system of my webhost hides .htaccess files even though they are allowed and do operate - and so I have checked back on local copies I have of my .htaccess files and none of them have the code as to how this works - and I've forgotten how I did it!!
Essentially, I am using wildcards so that anything after mt?page= will actually be showing PageParser.php?file= but without having the PageParser.php showing within the URL (and this is the important bit, because the index.php on my site root is actually sent through PageParser.php first so that anything which shouldn't be there is wiped out before the end user sees it) - so how can .htaccess redirect/rewrite the URL so that any link to /mt?page= show the file located at /PageParser.php?file= without changing the URL the user sees?
RewriteEngine On
RewriteRule ^(.*)mt?page=(.*)$ $1PageParser.php?file=$2
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^page=([^&]+)
RewriteRule ^mt$ /PageParser.php?file=%1.php [NC,L]
This rule will rewrite (internal redirect) request for /mt?page=hello to /PageParser.php?file=hello.php without changing URL in browser.
Your source URL example (www.website.com/mt?page=index) has index while target URL (www.website.com/PageParser.php?file=index.php) has index.php. The above rule will add .php to the page name value, so if you request /mt?page=hello.php it will be rewritten to /PageParser.php?file=hello.php.php.
If there is a typo in your URL example and page value should be passed as is, then remove .php bit from rewrite rule.
The rule will work fine even if some other parameters are present (e.g. /mt?page=hello&name=Pinky) but those extra parameters will not be passed to rewritten URL. If needed -- add QSA flag to rewrite rule.
This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
P.S.
Better write no explanation (I knew it/I did it before .. but now I forgot how I did it) than having these "excuses". While it may be 100% true, it just does not sound that great.

.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.

Resources