Apache .htaccess: Serving .css files from separate domain? - apache

I'm trying to serve CSS files for my site from a separate domain.
I've got the following entry in my site's .htaccess (in the document root):
RewriteRule ^templates/*\.css$ http://css.mysite.com/templates/$1 [R=301,L]
With the intention of simply matching:
http://www.mysite.com/templates/default/styles/main.css
… and sending an HTTP 301 redirect to:
http://css.mysite.com/templates/default/styles/main.css
But what's actually getting called is:
http://css.mysite.com/templates/.css
(I'd like to duplicate the above for *.js and serve them from js.mysite.com and all png & jpgs and serve them from img.mysite.com.)
Any insights would be fantastic. I've got to admit, htaccess rewrite rules always seem to elude me.

Try this and let me know:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^css\.
RewriteRule ^templates/(.*)\.css$ http://css.mysite.com/templates/$1.css [R=301,L]
or a more "dynamic way"
RewriteCond %{HTTP_HOST} ^(www\.)?mysite
RewriteRule ^templates/(.*)\.(js|css)$ http://$2.mysite.com/templates/$1.$2 [R=301,L]
RewriteRule ^templates/(.*)\.(jpg|gif|swf|jpeg|png)$ http://imgs.mysite.com/templates/$1.$2 [R=301,L]
This one will check for JS and CSS and use the subdomain based on the file extension: so .js will go to js.mysite.com and .css will go to css.mysite.com and same for images but these goes to imgs.mysite.com instead.
or be even more generic:
RewriteCond %{HTTP_HOST} ^(www\.)?mysite
RewriteRule ^templates/(.*)$ http://media.mysite.com/templates/$1 [R=301,L]
You redirecting everything that start with templates

Related

301 redirect dynamic cgi generated urls using .htaccess

I'm having trouble making sense of this .htaccess file I've inherited at my new job. I'm a graphic designer / marketer learning web development. Our site has a bunch of duplication errors and I want to rename our links and have them 301 redirected.
This is the first part of the .htaccess file:
RewriteEngine Off
RewriteCond %{HTTP_HOST} !^blog.asseenontvhot10.com$ [NC]
RewriteCond %{REQUEST_URI} !/sitemap\.xml
AddType text/html .shtml, .html, .htm
AddHandler server-parsed .shtml, .html, .htm
AddHandler application/x-httpd-php5 .html .htm .shtml
#Options FollowSymLinks Includes -Indexes
From what I understand the first part links our blog to the main site, and I'm not sure what the next line does.
The addtype line and the ones that follow enable .htaccess to read/write those filetypes?
Here is the part that I believe is relevant and what I need to change:
RewriteEngine on
rewritecond %{http_host} ^[^.]+\.[^.]+$ [nc]
rewriterule ^(.*)$ http://www.%{http_host}/$1 [r=301,nc]
RewriteRule default.html /home.html
RewriteRule contact\.html /cgi-bin/commerce.cgi?display=contact
RewriteRule about\.html /cgi-bin/commerce.cgi?display=about
RewriteRule register\.html /cgi-bin/commerce.cgi?register=action
RewriteRule products\.html /cgi-bin/commerce.cgi?listcategories
RewriteRule parent_category/(.*)/(.*)/ /cgi-bin/commerce.cgi?listcategories=action&parent=$1
RewriteRule category/(.*)/(.*)/(.*)\.html /cgi-bin/commerce.cgi?search=action&category=$1&page=$3
RewriteRule category/(.*)/(.*)/ /cgi-bin/commerce.cgi?search=action&category=$1
RewriteRule category/(.*).html /cgi-bin/commerce.cgi?search=action&page=$1
RewriteRule product/(.*)/(.*)/ /cgi-bin/commerce.cgi?preadd=action&key=$1
RewriteRule basket\.html /cgi-bin/commerce.cgi?display=basket
RewriteRule log-in\.html /cgi-bin/commerce.cgi?login
RewriteRule home\.html /cgi-bin/commerce.cgi?display=home
RewriteRule search\.html /cgi-bin/commerce.cgi?display=search
RewriteRule all-items\.html /cgi-bin/commerce.cgi?search
RewriteRule update_user\.html /cgi-bin/commerce.cgi?displayuser=action
RewriteRule order_history\.html /cgi-bin/commerce.cgi?orderhistory
RewriteRule logout\.html /cgi-bin/commerce.cgi?logout
RewriteRule specials\.html /cgi-bin/commerce.cgi?search=action&keywords=nav_specials
I know the first part forces www. and does a 301 redirect, even though I haven't seen this way of accomplishing it on any guide I've read. The first thing I really need to change is making the home page 301 redirect from:
http://www.asseenontvhot10.com/cgi-bin/commerce.cgi?display=home
to
http://www.asseenontvhot10.com/
So far as I can tell the RewriteRule makes this an option but dosn't force it. Will adding [r=301] to the end of the rewriterule work?
I've also read that there is code you need to add for .cgi to work as .php does but I'm totally lost there.
Update:
I tried adding this line to the bottom -
Redirect 301 /cgi-bin/commerce.cgi?display=home /
and I got a 500 error.
2nd Update:
I added this code:
RewriteRule about\.html /cgi-bin/commerce.cgi?display=about
RewriteCond %{QUERY_STRING} ^display=about$
RewriteRule ^cgi-bin/commerce\.cgi$ /about [R=301]
And it 404'd. When I replaced the .htaccess file with the back up and the about page still 404's... Now I'm really at a loss and don't know how to revert the site. I don't even know where the redirect would be stored outside of the htaccess file once it's reverted.
3rd Update: (making progress)
I added this code:
RewriteRule about\.html /cgi-bin/commerce.cgi?display=about
RewriteCond %{QUERY_STRING} ^display=about$
RewriteRule ^cgi-bin/commerce\.cgi$ /about\.html? [R=301]
and I got a redirect loop. It was finally redirecting to about.html which I thought the first rule renamed /cgi-bin/commerce.cgi?display=about to, but it redirected to it over and over again.
The Redirect directive in Apache requires that the redirect destination be a fully qualified URL with a scheme and a URL. So this would work:
Redirect 301 /cgi-bin/commerce.cgi http://www.yourdomain.com/
However, I don't believe that Redirect can check the query string, so in order to only apply this redirect if the query string is "display=home" you will probably have to use mod_rewrite like this:
RewriteCond %{QUERY_STRING} ^display=home$
RewriteRule ^cgi-bin/commerce\.cgi$ / [R=301]
The RewriteCond checks that the query string exactly equals "display=home" and the RewriteRule will only trigger if this condition is met. And in the RewriteRule the [R=301] flag instructs mod_rewrite to redirect the visitor's web browser to the target path, / in this case.
By the way, looking at the existing conditions in your .htaccess file, I can't see how your users are seeing the /cgi-bin/commerce.cgi path in their web browser. All of those RewriteRule directives are for silent rewrites. This means that the user will continue to see the "friendly" URL in their web browser, but in the background Apache will actually find the file located at the /cgi-bin/commerce.cgi path and return the content or result of that file to the user. So your users should already be seeing the friendly version of the URL. If what you are trying to do instead is silently rewrite a request for "/" (site root) so that Apache silently rewrites the path and actually fetches the content in the real file /cgi-bin/commerce.cgi?display=home then you probably want this instead:
RewriteRule ^$ /cgi-bin/commerce.cgi?display=home
This will only match a request for the root directory (with no filename specified) and will silently fetch and return the content which results from calling the commerce.cgi file with a "display=home" parameter.

apache redirect 301 to .php file extension

for our client project we need to redirect all url calls to the matching .php file extension. this is done for SEO (google) ranking. avoiding indexing both url (with *.php and without)
we try to do this using the .htaccess file (shared host) but it seemed to only work for redirects that have a different url and not to the one that just adding the ".php" extension.
different url works
abc -> abc2.php
same url doesnt work
abc -> abc.php
here is our code sample:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.abc\.co.uk
RewriteRule (.*) http://www.abc.co.uk/$1 [R=301,L]
# this doesn't work
Redirect 301 /test http://www.abc.co.uk/test.php
# this redirect works as the url is different
Redirect 301 /test-abc http://www.abc.co.uk/abc.php
we host with 1&1 shared host server.
php version is 5.5
I also tried this code that redirects but the page not loading now:
RewriteRule ^(.+)\.(.*)$ http://abc.co.uk/$1.php [R,NC]
also noticed url without file extension (.*|html) will not redirect
Try this .htaccess in DocumentRoot:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.abc\.co\.uk$ [NC]
RewriteRule (.*) http://www.abc.co.uk/$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /$1.php [L,R=302]
This appears to be a 302 re-direct, which is not SEO freindly. I tested the above and it doesnt work, also, if it did I wonder what would happen to url's that did not require the .php extension on the URL.
Basically we're trying to get a 301 re-direct working, but it seems to be conflicting with the rules set up that contain the same url. (IE abc > abc.php)
I wonder if there could be some kind of Apache setting or even and unlikely a PHP setting? the 301's work on any other linux server that I try.

htaccess adding WWW and changing filename in subdirectory

I know similar questions have come up, though often without a working answer. I'm hoping to have better luck!
I have an .htaccess file in my root directory adding "www" to everything:
RewriteEngine on
RewriteCond %{SERVER_NAME} ^mysite.org
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=permanent,L]
This generally works fine. I have a subfolder (/myquiz/) in which the old index.html file has been replaced with index.php. I know there are external links to /myquiz/index.html, so I want to make sure those redirect. Leaving index.html in place and trying to redirect from that led to some odd behavior, but adding an .htaccess in that directory works for that:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html?$ /myquiz/index\.php [NC,R]
Trying to load index.html redirects to index.php as hoped for, and the WWW gets added if needed. But requesting mysite.org/myquiz/index.php directly does not add the WWW.
I tried adding "RewriteEngine inherit", but that resulted in calls getting redirected to my root folder instead. A great trick if I want to make a subfolder inaccessible, but not helping here. I also tried just adding the code from my root .htaccess into the beginning of my subfolder's .htaccess, but that worked no better.
Any ideas?
You shouldn't need to add another htaccess file in the myquiz folder. This should work in the htaccess file in the root of the site. Remove the htaccess file in myquiz and try this.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.org
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=301,L]
RewriteRule ^myquiz/index\.html$ /myquiz/index.php [R=301,L]
Also I wouldn't use %{SERVER_NAME} unless your are sure the name is set properly in the config file. Then it can be more reliable than HTTP_HOST, otherwise I would instead use %{HTTP_HOST}.
I think inherit would work if you add an L flag to the rule that you have in your myquiz folder:
RewriteRule ^index\.html?$ /myquiz/index\.php [NC,R,L]
So that it redirects first, then the inherited rule (the www) gets applied after.
You could also just put both rules in the same file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.org$ [NC]
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=permanent,L]
RewriteRule ^myquiz/index\.html$ http://www.mysite.org/myquiz/index.php [R=permanent,L]

.htaccess refuses to redirect URLs without file extensions - straight to 404

So I've been tinkering. Apache's .htaccess was redirecting my WWW client just fine (if there is no WWW present, 301 to the same URL with a WWW included), and it was redirecting things with file extensions (i.e. index.html will redirect to index.php). It's only URLs that are without an extension. It refuses to decipher them.
My current file:
RewriteEngine On
RewriteBase /
# Clean URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ $1.php [NS,L,R=permanent]
# Force WWW
RewriteCond %{HTTP_HOST} !^www\.thevgc\.net$
RewriteRule ^(.*)/?$ http://www.thevgc.net/$1 [NS,L,R=permanent]
I've been trying to figure this out all night long now. mod_rewrite is enabled, and I had to create a symbolic link to /etc/apache2/mods-enabled/rewrite.conf before actually making the file, which consists of the following:
RewriteEngine On
Thoughts on this dilemma of mine?
SO! I found the answer to my problem. Looks like if you run the command a2dismod negotiation it magically starts working, or at least, it did in my case. Problem solved, case closed, thread done. The mystery has been solved.

ReWrite Rules Issue

I seem to be having an issue with my Apache Rewrites
RewriteEngine on
RewriteBase /
RewriteRule ^wordpress/?$ / [NC,L,R=301]
RewriteRule ^/$ wordpress/ [NC,L]
I simply need to remove /wordpress from the URL as I have pages within Wordpress I want to be seen as the main directory
At the moment the urls are
domain.com/wordpress/blog
I'd rather not have /wordpress, rather domain.com/blog
Any help?
RewriteEngine on
RewriteBase /
RewriteRule ^wordpress/(.*)$ blog/$1 [L]
At the moment the urls are
domain.com/wordpress/blog
I'd rather not have /wordpress, rather domain.com/blog
So it looks like you want to redirect the browser if someone makes a request for domain.com/wordpress/ to a URL without the wordpress bit, then internally rewrite the wordpress bit back into the URI? That's definitely do-able but if you have wordpress rewrite rules somewhere they're not going to play nicely with each other at all.
Any rules in the /wordpress directory will supercede any rules you put in the document root, which is where these rules need to go, and your remove-the-wordress-from-URI rules will be completely ignored. Even if you have rule inheritance turned on, the rules in the /wordpress directory will get executed first.
If all of your wordpress rules are actually in the document root's htaccess file, then just make sure to put these before the wordpress ones:
RewriteEngine on
RewriteBase /
# redirect the browser if someone makes a request for domain.com/wordpress/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /wordpress/
RewriteRule ^/?wordpress/(.*)$ /$1 [L,R=301]
# internally rewrite the wordpress bit back into the URI
RewriteRule %{DOCUMENT_ROOT}/wordpress%{REQUEST_URI} -f [OR]
RewriteRule %{DOCUMENT_ROOT}/wordpress%{REQUEST_URI} -d
RewriteRule ^(.*)$ /wordpress/$1 [L]