URL Rewriting in htaccess - .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.

Related

Redirecting a "subfolder" to a new URL; Joomla and .htaccess

This should be simple and I'm not sure why I can't find anything on it...
Simply put
http://www.abcd.org/efg/ (this is a single article in a Joomla site, the rest of the domain needs to stay active, so just the efg part is what I need redirected.)
needs to redirect to
https://www.xyx.com/lmn-op-qrs/
Is this possible in a Joomla site (Joomla Redirect component fails)?
I see plenty for redirecting within a certain domain, and even redirecting the entire domain to a new domain, but nothing about redirecting part of a domain to a new URL.
I'm sure I could pop in a PHP page to the /abcd.org/efg/ directory and redirect it that way, but wondering if it was possible using the .htaccess file.
Thanks!
You may try this rule as your topmost rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+efg[?\s/] [NC]
RewriteRule ^ https://www.xyx.com/lmn-op-qrs/ [L,R=301]
put this rule in top of .htaccess (on http://www.abcd.org/ root directory)
RewriteEngine On
RewriteRule ^efg/$ https://www.xyx.com/lmn-op-qrs/ [R=301,L]

How to redirect a new page (wrong URL) to another page?

I'm new at programming. We have an office project, the website's URL is www.project.com.ph (sample name), this is already a live website from the client. But the released printouts have the instructions for the users to go to www.project.com/ph which is wrong and we can't reprint the material since it already reached plenty of event places.
Now the problem is, we need to redirect to www.project.com.ph automatically if the users type in the browser's address bar www.project.com/ph. I ask if this is possible without any kind of CMS or Wordpress and how to actually do it? We bought a new domain www.project.com for this. Any kind of help is appreciated.
Try the following near the top of your .htaccess file in the root of www.project.com. This works OK (although marginally less efficient) if both domains are pointing to the same place, since it specifically checks that we are requesting the "wrong" domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?project\.com$ [NC]
RewriteRule ^ph/?(.*) http://www.project.com.ph/$1 [NC,R=302,L]
This will redirect requests for www.project.com/ph (no slash), www.project.com/ph/ (with a trailing slash) and www.project.com/ph/<whatever> to http://www.project.com.ph/<whatever>.
This is a temporary (302) redirect. Change it to a permanent (301) only when you are sure it's working OK.
From kj.'s answer on a similar question, here
In your .htaccess for www.project.com, this should do the trick.
RewriteEngine on
RewriteRule ^(.*)$ http://www.project.com.ph/ [R=permanent,NC,L]
This will redirect any request to project.com to the domain http://www.project.com.ph/
To include the path after the /ph/` you can use this.
RewriteEngine on
# redirect including path after ph/ (e.g. project.com/ph/directory/file.php to project.com.ph/directory/file.php
RewriteRule ^ph/(.*)$ http://www.project.com.ph/$1 [R=permanent,NC,L]
# redirect any other requests to project.com.ph index
RewriteRule ^(.*)$ http://www.project.com.ph/ [R=permanent,NC,L]
You can redirect (301 redirect) the URL using RewritrRule in .htaccess file
RewriteRule "http://www.project.com/ph/(.*)" "http://www.project.com.ph/$1" [L,NC,R=301]

htaccess multiple 301 redirects

I am looking for some help with setting up some redirect rules in my htaccess file. I have switched the site to run on Codeigniter, and basically all pages ending in .php, now just have that ending removed, ie www.mysite.com/foo/bar.php is now www.mysite.com/foo/bar
I have tried just listing the redirects like so:
Redirect 301 http://mysite.com/foo/bar.php http://mysite.com/foo/bar
Redirect 301 http://mysite.com/foo/bar/bar.php http://mysite.com/foo/bar/bar
Redirect 301 http://mysite.com/foo/bar/foo.php http://mysite.com/foo/bar/foo
etc (About 12 rules needed). Bizarrely, it works for the first rule but not the others? Am I not listing them correctly? Everything I've read tells me this is how it is to be written. So what am I doing wrong?
Additional Information that may be relevant:
My htaccess file already contains the following code (for removing 'index.php' from urls):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Not sure if it matters if this is placed before or after the rewrites? Also, the site in question is an addon domain. I am not entirely sure whether these rewrites should be placed in the .htaccess file in the public folder of the master site, or in the addon domain's subdirectory? (I've tried both, with the same result).
Help please! Thanks in advance.
Sorry, I've just solved this. I removed the base URL from each rule and now it works. ie:
Redirect 301 /foo/bar.php /foo/bar
Instead of:
Redirect 301 http://mysite.com/foo/bar.php http://mysite.com/foo/bar
Not entrirely sure why... need to read up.

Need .htaccess assistance

We have an existing site, let's call it ourdomain.com. We moved content over from a site that is shutting down, let's call it legacydomain.com. We pointed the legacy domain at our server to the /public_html/ folder.
What I need is an .htaccess that will redirect legacydomain.com or legacydomain/anything-here to ourdomain.com/legacydomain/ with nothing else appended to the URL.
However, I also need a few specific URLs to redirect to certain destinations, and they don't really follow a pattern. For example:
legacydomain.com/something.html to ourdomain.com/legacydomain/something.html
legacydomain.com/another.html to ourdomain.com/legacydomain/folder/another.html
This is what I have tried:
RewriteCond %{HTTP_HOST} ^www\.legacydomain\.com$ [NC]
RewriteRule (.*) http://www.ourdomain.com/legacydomain/$1 [R=301,L]
Redirect 301 /something.html http://www.ourdomain.com/legacydomain/another.html
It mostly works, but if I visit legacydomain.com/anything-here it doesn't even attempt to rewrite, it just keeps the domain the same and gives a 404. And also I have a feeling that even if it did work, something like legacydomain.com/anything-here/more-stuff would get rewritten as ourdomain.com/legacydomain/anything-here/more-stuff which I don't want.
Only other thing in the .htaccess is rewriting non-www to www, and the standard WordPress stuff. Any guidance would be greatly appreciated. Everything above should have an http:// and www in front for the examples, but it wouldn't let me post that many "links".
For each specific rewrite you would need two lines, as follows. Depending on your existing config you may need to add a slash at the beginning of the RewriteRule in front of something.html if this doesn't work.
RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule something.html http://ourdomain.com/legacydomain/something.html [R=301,L]
Then you would use a catch-all for everything else.
RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule (.*) http://ourdomain.com/legacydomain/ [R=301,L]
Personally, I would go for the simplest solution which doesn't use mod_rewrite. First, just redirect the specific pages to wherever they need to go.
Redirect 301 /something.html http://ourdomain.com/legacydomain/something.html
Redirect 301 /another.html http://ourdomain.com/legacydomain/another.html
Then, simply redirect everything else to the base URL.
RedirectMatch 301 (.*) http://ourdomain.com/legacydomain/
These must be put in your .htaccess file before the RewriteEngine on statement.

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/

Resources