Use of page to redirect - .htaccess

I need to set up a page to redirect from our old server to our new server, advising people of the new URL and to change their bookmarks. I know how to use the refresh meta tag in HTML to do this.
But I also want to set it up so that they would see the redirect page no matter what page they have navigated to on the old server. I see several solutions using 301 redirect.
How would I do both? We're running an Apache server on Debian server.

You need to use Apache an RewriteRule using mod_rewrite. This can be placed in an .htaccess file on your server’s root, or it can be placed directly into your Apache config file.
So let’s say your new redirect page—where you explain the site has moved and have set a refresh meta tag—is called redirect.php, then this is the Apache RewriteRule that should work for your needs.
RewriteEngine on
RewriteRule ^(.*)$ /redirect.php [L,R=301]
This will grab any URL on the site this RewriteRule is placed on & redirect them to /redirect.php. That /redirect.php can also be a full URL such as http://mysite.com/redirect.php or anything else.

Related

My htaccess passthrough rule redirects to the url instead

I'm trying to passthrough (not redirect!) an empty old page to its new location using an htaccess RewriteRule.
I essentially want the user to browse to mysite.com/page-old and to see that url in their browser but be delivered the content from mysite.com/page-new. The user should not be aware that the location changed.
RewriteEngine On
RewriteRule ^page-old/?$ /page-new [PT]
The actual result is that they are redirected to page-new instead.
I found the below on apache.org which seems to validate my code some, but this is giving me a 404 error.
Description:
Assume we have recently renamed the page foo.html to bar.html and now want to provide the old URL for backward compatibility. However, we want that users of the old URL even not recognize that the pages was renamed - that is, we don't want the address to change in their browser
https://httpd.apache.org/docs/trunk/rewrite/remapping.html
RewriteRule "^/foo\.html$" "/bar.html" [PT]
RewriteEngine On
RewriteRule ^example/my-stuff/$ /example/home/ [L,R=301]
check this answer as well
How to redirect a specific page using htaccess

Redirect one URL to another on same domain with htaccess

I am currently building a new website. The old website has different url's than the new one. Now i want to redirect, using htaccess.
Before going live, i want to test my rules locally.
The old website uses url's of this format:
www.domain.de/?content=whatevercontent
The new website uses url's of this format:
www.domain.de/index.php?content=differentcontentname
Which are rewritten (in the htaccess file, using several RewriteRules) to this format:
www.domain.de/nicecontentname
I tried Redirects like this:
Redirect http://domainfolder/?content=whatevercontent http://10.3.10.69/domainfolder/nicecontentname
This does not work.
After going live it should work like this:
Redirect http://www.domain.de/?content=whatevercontent http://www.domain.de/index.php?content=differentcontentname
..and then be rewritten to the nice url.
My Redirect-Rules just won't apply, i tried it in all combinations i could think of, with or without http, with or without the containing folder, using the already rewritten url or the actual one, etc..
Any ideas on this issue?
You can not use Redirect directive to manipulte querystrings. Here is a mod-rewrite example that works for QueryStrings :
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/\?content=this
RewriteRule ^$ /index.php?content=that [L,R]
This will temporary redirect /?content=this to /index.php?content=that
To make the redirect permanent (browser cacheable) change R to R=301.

How do I redirect a web URL using .htaccess

I want to redirect users who enter this website URL:
http://www.myWebsite.com/bananas
to:
http://www.myWebsite.com/fruits/bananas
I cant test it because I'm sending this to somebody.
I have these but I don't know for sure which one works:
RedirectMatch 301 http://www.myWebsite.com/bananas(.*) http://www.myWebsite.com/food/bananas $1
Options +FollowSymlinks
RewriteEngine on
rewriterule ^bananas(.*)$ http://www.myWebsite.com/food/bananas $1 [r=301,nc]
Please specify if you want to redirect or rewrite. The rules you are using serve different purposes and you used both in your example.
Redirect: Actually load a different site when entering the url (end up at url and content of /fruits/bananas)
Rewrite: Url stays the same but server provides rewritten content (url stays at /bananas, but show /fruits/bananas content)
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Also it is not clear if you only want one single directory to be redirected or all files that are contained in that directory.
Checkout this as a guide: http://www.htaccessredirect.net/
I believe you are looking for
Redirect 301 /bananas http://www.myWebsite.com/fruits/bananas
The HTTP 301 stands for Moved Permanently.
I haven't tested it, though.

Simple RewriteRule with nginx

I'm trying to write a RewriteRule to make a simple url. I want users to be able to type enter
www.example.com/somepage
and have it take them to
www.example.com/abc/somepage.php
How can this be done in .htaccess?
I've tried these to no avail:
RewriteRule ^somepage$ abc/somepage.php [L]
RewriteRule ^/somepage$ /abc/somepage.php [L]
What am I doing wrong?
Edit: nginx can also handle rewrites in its configuration files (and I think is actually preferred), which was my case (see my answer below). HTH
If you want the user's web browser to be redirected to your PHP page then you need the R=301 flag at the end of the RewriteRule.
But if you want the request to be silently rewritten (so that Apache knows where to find the resource, but the user's web browser just sees the "clean" URL) then you don't want the `R' flag.
Try the following:
RewriteRule ^somepage$ /abc/somepage.php
This will only rewrite a request for /somepage so that Apache fetches the content from /abc/somepage.php instead.
Our nginx server was utilizing .htaccess files (as I implemented rewrites there before), but now it is handling the rewrites in its configuration (.conf) files (which is the recommended method).
I do not have permission to modify the .conf files, so I won't post any untested code.. Hope this helps somebody!

Website pages with slashes

Just building a site using php. I would like to know how to create pages without having to make ".php" links for every page. For example I want to build the site to be as follows: www.mysite.com/products instead of www.mysite.com/products.php. (Further example www.mysite.com/products/headphones )
An example of what I am looking for is as follows: http://www.starbucks.ca/store-locator
Any idea how starbucks is doing this? It isn't making a separate folder and index for every page is it?
Thanks in advance!
Jason
You can use URL rewriting to do this. You can create a .htaccess file, place it in the root directory of your website with the following content (as an example):
RewriteEngine On
RewriteRule ^tutorial/([0-9]+)/$ index.php?tutorial=$1
RewriteRule ^page/store-locator/$ index.php?page=store-locator
RewriteRule ^$ index.php
These are some basic rules that tell the web server to rewrite a URL without .php to a URL that does have .php.
In the above example, when you are visiting example.com/tutorial/3/, it will actually visit example.com/index.php?tutorial=3 and then your index.php will display the contents. It's similar for the store-locator page.

Resources