url redirecting through htaccess - .htaccess

i have hosted two sites on same server and i mange them giving folders for each domain. i want to site comes as
example.com/example
example1.com/example1
i want to come
example.com and example.com
i come to know that htaccess url redirecting can do this but i have less idea about it.
if there is some tutorial on php regular expression please let me know.
some help is appreciable.
edited
i solved this problem by putting .htaccess file in root and writing:
RewriteEngine On
#for example.com
RewriteCond %{HTTP_HOST} example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/example/.*$
RewriteRule ^(.*)$ /example/$1 [L]
when i hit example.com it works fine now. it access the folder /example. but i had a problem on a certain domain. i m using codeigniter and i have to redirect its request also so that it does not show
example.com/example/index.php/controller/
i want to show this:
example.com/controller/
hope some experts can help.

The .htaccess url redirecting is setup as a file in your main directory and your code can look something like this:
# These redirects should do the trick :-)
Redirect example.com/example http://www.example.com
Redirect example.com/example1 http://www.example.com

Related

Permanent redirecting to different domain via .htacces

I got two domains. Lets say dom.com and dom.net.
Now I got alias on dom.net to dom.com, so when I come to dom.net it shows me dom.com - this is set on admin's webpage.
But I need when somebody comes from dom.net redirect him to dom.com/test/example.html.
My .htacces file is blank now.
Is it possible to do this using .htacces and how can it be done?
I got it!
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} dom.net$
RewriteRule ^(.*)$ http://www.dom.com/test$1 [R=301,L]
this code redirect me to test folder but still don't know how to redirect to .html page

How to create a 301 redirect in .htaccess for *.olddomain.com to *.newdomain.com

I've checked many ways for redirecting old domain to new domain. But I did not find any way for that.
All my subfolder of my site is using subdomain address, such as myfolder.domain.com, so it built many many subdomains. Now I want to redirect all of them to new domain.
redirect *.olddomain.com to *.newdomain.com. Can anybody who know .htaccess can help me with this? I'd really really appreciate it! Thanks!
redirect *.olddomain.com to *.newdomain.com. Can anybody who know .htaccess can help me with this?
As long as all of your *.olddomain.com domains all point to the same document root, you can just put these rules in the htaccess file in that directory:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://%1.newdomain.com/$1 [L,R=301]
This will redirect the browser from:
http://foo.olddomain.com/bar.html to
http://foo.newdomain.com/bar.html

Problems with htaccess Redirect (Page on one domain to same page on another)

I'm trying to redirect a few pages to a new domain and I have done this before but for some reason I can't get the code to work.
RewriteEngine On
Redirect 301 http://domain.com/page1.html http://domain2.com/page1.html
Can anyone see where I'm going wrong?
In .htaccess file the below code will ensure that all your directories and pages of your old domain will get correctly redirected to your new domain.
The .htaccess file needs to be placed in the root directory of your old website
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
Since you say you only want to direct a FEW of your pages and not all of them, you can do:
RewriteEngine On
Redirect /~myaccount/oldpage.html http://www.newsite.com/newpage.html
You specify the path to the page on your current server followed by the URL to redirect to.
OR you can do:
RedirectMatch 301 ^/oldpage\.html$ http://www.newsite.com/newpage.html

How do I rewrite the url?

Could someone tell me how to rewrite this URL. I have looked at a lot of questions on stackoverflow but they seem to be missing my answer.
RewriteEngine On
That is what I have... its a bit poor.
I need to rewrite url's if they do not point to a directory.
I need to do this...
any.domain.com/pages/some-page-slug/login
To be rewritten to the correct url of...
any.domain.com/pages/login.php?page=32
Does anyone have any ideas on how this can be achieved?
1) Rewriting product.php?id=12 to product-12.html
It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
2) Rewriting product.php?id=12 to product/ipod-nano/12.html
SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
3) Redirecting non www URL to www URL
If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
5) Redirecting the domain to a new subfolder of inside public_html.
Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1
TO do this you need to write a front controller.
See here, here, here, and here.
Alternatively in Apache you can rewrite this
any.domain.com/pages/32/login
or this:
any.domain.com/32/login
or even this:
any.domain.com/some-slug/32/login
to this:
any.domain.com/pages/login.php?page=32
One way or another to do this with only apache you need to supply the page id in some fashion. Keep in mind even with format any.domain.com/some-slug/32/login the content of the slug is irrelevant and won't necessarily link to the correct page. Which I imagine is undesirable and bad for SEO.
Another alternative is using RewriteMap. But this will be tricky and require reloading apache configurations whenever a page/slug is created/edit.
I understand that pages and login are static in this case and some-page-slug is changing. And you always want to redirect to static page /pages/login.php?page=32
So this is how to do it:
1) Rewrite
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32
or 2) Redirect Pernament
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=301,L]
or 3) Redirect Temporary
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=302,L]
Here is great article about htaccess trics
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/

URL Rewriting in htaccess

I just rebuilt a site that is currently located at
http://domainnamehere.com/v2
The existing site is located at
http://domainnamehere.com/
I need to set up a redirect so that when someone goes to the old site, it redirects to the new site. I did this on the htaccess file:
Redirect /index.html http://domainnamehere.com/v2/
But this only redirects the homepage, not the rest of the site. I also have another directory that is linked to a separate domain. The above htaccess code redirects http://anotherdomain.com to http://domainnamehere.com/v2. I do not want it to redirect other domain names. THe reason it is redirecting the other domain name is because the files are located in a directory on domainnamehere.com. I hope that makes sense!
If someone goes to http://domainnamehere.com/somethinghere I also want it to redirect to the 404 error page that would be located at http://domainnamehere.com/v2/somethinghere
Basically, I need proper htaccess code for this situation!
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domainnamehere\.com [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/v2/$1 [R=301,L]
Note: jfrobishow technically answered this first, this is just a fix.
Edit: Added optional www.
This should forward {whatever is after you slash} to /v2/{whatever is after you slash}
RewriteCond %{HTTP:Host} ^domainnamehere\.com$
RewriteRule /(.*) domainnamehere\.com/v2/$1
EDIT: this is for isapi rewrite, but the syntax is similar if you are using mod_rewrite.

Resources