Rewrite whole URL with .htaccess - apache

I have an URL with a subdomain that looks like this:
test.my-site.com/
Let's say I want to add forums section to it, which works under:
test.my-site.com/forums/
That works but I want to now rewrite the URL so that every time someone visits the forums section instead of test.my-site.com/forums he sees forums.my-site.com/ in the URL.
I'm trying to do that through the .htaccess like that:
RewriteRule test.my-site.com/forums/(.*)$ forums.my-site.com/$1 [R=302,NE,L]
However, this has absolutely no effect. Any suggestions?

Use:
RewriteEngine on
# Redirect to forums subdomain
RewriteCond %{HTTP_HOST} ^test\.my-site\.com$ [NC]
RewriteRule ^forums/(.*)$ http://forums.my-site.com/$1 [NC,R=302,NE,L]
# Rewrite from forums subdomain to forums folder
RewriteCond %{HTTP_HOST} ^forums\.my-site\.com$ [NC]
RewriteRule ^(.*)$ /forums/$1 [NE,L]

You can create a subdomain forums and use public_html/forums as a directory root for it. Create index.html file in the subdomain directory and use the below html tags:
<html>
<head>
<title>you title goes here</title>
</head><frameset rows="100%" scrolling="yes" border="0">
<frame src="test.my-site.com/forums">
</frameset></html>
try it and let me know any issue

Here is what i suggest
RewriteRule ^mySite.com/([forum|blog])$ $1.mySite.com/ [QSA,L]

Related

.htaccess redirect from directory to subdomain without changing url

I would like to redirect /blog/ to another project. Without changing the url.
The hole thing almost works with the redirect rules below and using Apache mod_proxy, but only if it hits the index itself on /blog/ it DOES redirect to blog.website.nl.
RewriteCond %{HTTP_HOST} ^website\.nl [NC]
RewriteRule ^(.*)$ http://blog.website.nl/$1 [P,L]
When I visit website.nl/blog/category/test/
it works fine!
So why is this still changing the url on /blog/ and how to fix that?
Thanks! This has been quite a puzzle for me so far.
Update:
I managed to get it working. Instead of adding a .htaccess file to blog folder I added this code to the root /htaccess file. Now it all works!
RewriteCond %{HTTP_HOST} ^website\.nl[NC]
RewriteRule ^/?blog/(.*)$ http://blog.website.nl/$1 [P,L]

RewriteRule for SEO in a SPA not working

I wish to implement SEO for my single page application website (assume it to be at www.myexample.com)
After reading Google's documentation (https://developers.google.com/webmasters/ajax-crawling/docs/getting-started) I started by attempting this for the website's home page.
That means,
www.myexample.com?_escaped_fragment_=
should 'map to'
www.myexample.com/staticIndex.html
I added the following to .htaccess file in the website's public_html folder:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=
RewriteRule ^_escaped_fragment_= /staticIndex.html [PT]
After this change, when I try the URL www.myexample.com?_escaped_fragment_= in browser, I expected the browser to show me content of file staticIndex.html
Instead, it shows me content of www.myexample.com (index.html) and the browser URL remains unchanged to www.myexample.com?_escaped_fragment_=
Could someone help me with the RewriteRule?
Thanks.
try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=$
RewriteRule (.*) staticIndex.html [PT]

Exclude image urls from redirection using htaccess

My old blog was at www.example.com/myblog
I changed my server and I want to use www.example.com/blog
To do this I used htaccess code
Redirect 301 /myblog http://www.example.com/blog
This helped in redirecting the blog home page and individual blog posts.
The images were put in a folder named myblog in the new server.
So the image url is www.example.com/myblog/wp-content/uploads/2010/04/something.jpg
But the htaccess I wrote above redirects this to www.example.com/blog/wp-content/uploads/2010/04/something.jpg which returns 404.
Can anyone help me to write htaccess code for redirecting blog post urls only excluding the blog image urls?
Instead of using mod_alias and the Redirect directive, try mod_rewrite and add some conditions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?myblog/(.*)$ http://www.example.com/blog/$1 [L,R=301]
# redriect the index
RewriteRule ^/?myblog/?$ http://www.example.com/blog/ [L,R=301]

Rewrite URLs Using .htaccess

Referring to the article here I created my .htaccess file with the following lines
RewriteEngine on
RewriteRule ^/product/([0-9]+)/(.*)$ /index.php?route=product&product=$1 [L]
but this doesn't work and I get a 404 error.
I want URLs like
http://localhost/product/12/some-random-text.html
to be redirected to
http://localhost/index.php?route=product&product=12
I am using GoDaddy's Linux hosting
Try changing your RewriteRule to:
RewriteRule ^product/([0-9]+)/(.*)$ /index.php?route=product&product=$1 [QSA,L]

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

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