I'm 90% there but I'm missing something obvious. I am doing local web development of a site. The media directory of the site I'm working on is huge - upwards of 15 GB. Instead of copying that and having to update it periodically just for the sake of viewing this images in my local environment, I want to redirect any requests into the media/ directory back to the production site.
From this...
localhost:8888/sitefolder/media/*
To this...
www.productionserver.com/media/*
This is what I have so far but I think I need to use the Rewrite Condition directive:
RewriteRule ^media/(.*)$ http://www.productionserver.com/media/$1[QSA,R=301,L]
Thank you in advance.
Some syntax issues, you need a space after $1 and QSA isn't needed:
RewriteEngine On
RewriteRule ^media/(.*)$ http://www.productionserver.com/media/$1 [NC,R=301,L]
Make sure this is placed in sitefolder/.htaccess
Make sure this is first rule after RewriteEngine On
Related
I have:
subdomainaaa.mydomain.com
subdomainaaa.mydomain.com/anything/whatever
subdomainbbb.mydomain.com
subdomainbbb.mydomain.com/anything/whatever
...
...
anysubdomain.mydomain.com ...
Practically wild card subdomain.
And I need rewrite rule to make it all:
www.mydoamin.com/and_whatever_goes_afterwords
Basically, how to redirect zillion subdomains to www, leaving anything that goes after .com/ as it is.
I must do some sort of permanent redirection because I can't lose old links, just to redirect them to new ones with www instead of subdomains
Please, I was looking all over the internet for this, and I get only chunks of regex which I don't know how to put together, and whatever I mix in my .htaccess file - I get server error. Or, I manage to redirect anything just to the homepage www.mydomain.com. I hope I was clear. I am a regex newbie (wannabe), so, please give me the whole chunk to put in .htaccess.
Or, if you think I am doing something wrong - give me suggestion how to accomplish my goal.
Thanks a lot guys!!
You may use this rule in your site root .htaccess:
RewriteEngine On
# add www and keep same URI
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.(mydomain\.com)$ [NC]
RewriteRule ^ http://www.%1%{REQUEST_URI} [R=301,L,NE]
I am trying to make a .htaccess file that is basically a wildcard setup. I have a folder structure as below.
public_html/sites is the root directory and in this folder there are two sub directories:
public_html/sites/brand (there are many brand folders, just used brand as example)
public_html/sites/brand/event (there are many events in a brand folder, just used event as example)
I have files in each event folder such as index.php and media.php
I have been unsuccessful in rewriting the media page to the URL structure I am looking for. Below is my current .htaccess file.
RewriteEngine On
RewriteRule ^media/([^/]*)$ /media.php?unqid=$1 [L]
So the expected URL I am wanting is /testbrand/4177/media/123456. I will have serveral brands and several events under each brand so I am needing some sort of wildcard if possible. Any help is appreciated. Thanks in advance.
If you are wanting to have a URL like this
http://example.com/testbrand/4177/media/123456
And your brands are dynamic, then your rewrite should probably look something like this.
RewriteRule ^(?:[^/]+)/(?:[0-9]+)/media/([0-9]+)/?$ /media.php?unqid=$1 [L]
Based on your current internal URL you'll only be sending the id number that comes after /media/ to PHP. If you have more info you need to send, then you need to update your internal URI parameters.
You can use this rule in your root .htaccess:
RewriteEngine On
RewriteRule ^([^/]+/[^/]+)/media/(\d+)/?$ $1/media.php?unqid=$2 [L,NC,QSA]
This will load /testbrand/4177/media.php?unqid=123456 when you request /testbrand/4177/media/123456
I'd like to use the same robots.txt for staging and production thus I'd like to add an condition if HTTP_HOST = staging.mydomain.com so that only the staging environment is forbitten for robots?
Is this possible?
If not, what would be the right syntax for .htaccess to have a second file like robots_staging.txt and letting the webserver do the switching?
Thx, I appreciate your help!
Actually, what you seem to need is two robots.txt files—one for each domain. Then, in .htaccess, you direct the request to either file depending on which domain it was made on.
You can do it like this:
RewriteCond %{HTTP_HOST} staging.mydomain.com
RewriteRule ^robots\.txt robots_staging\.txt [L]
Anyone asking for robots.txt on the production site gets it without a rewrite
Anyone asking for robots.txt on the staging domain gets redirected to robots_staging.txt
In robots_staging.txt, place all the deny rules you like
I have an old site to clean up that has many pages with this ugly url format:
/index.php?option=com_content&view=article&id=47&Itemid=63
How do I write a rule in the .htaccess file that redirects any url that starts with ?option= to root /
Any suggestions would be great.
Thanks,
Why bother with rewrite rules, if you can also patch it in the index.php file itself which presumably still exists within the new site?
if(!empty($_GET['option']))
{
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://my.domain.tld/');
die;
}
This also correctly catches URLs that historically existed but had parameters switched or prefixed so that option wasn't at the beginning, and allows you to more flexibly patch if one day you need to use an option GET-parameter again.
Mod_rewrite is cool, but not the solution to every redirection problem.
Having said that, if you truly insist on the .htaccess solution, try this:
RewriteCond %{QUERY_STRING} ^option=(.*)
RewriteRule . / [L,R=301]
It tests the query string and then redirects all requests if it matches.
I'm trying to clean up our website. I'm using ISAPI Rewrite. I have figured out the regex I need to select the files in question.
^(?!.*?web_content.*?).*?\.pdf
I want to redirect all pdf requests to look in web_content/pdf/. So The pseudo rule I want is
redirect all requests to pdfs that aren't already being requested from
web_content/pdf. Drop off the original folder path.
/somefolder/this/mycool.pdf ==> /web_content/pdf/mycool.pdf
My question is how would I actually create the Mod rewrite rule? I don't know how to correctly do the replacement command. I also hope this rule wont affect external pdf links on our site.
This should work:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/web_content/pdf/
RewriteRule ^(.+\.pdf)$ /web_content/pdf/$1 [L]
Helicon ISAPI_Rewrite v3 is pretty much the same as Apache's mod_rewrite (except few advanced things), therefore almost all rules that work on Apache will work here as well.