php htaccess redirect - php

i have a site in php.
now i want to redirect my older site url to new url.
whenever user enter my old site url in address bar using browser url must be redirect to my new url.
How can i do this using .htaccess file.
Thanks in advance.

If you have a page by page mapping you can do it like this:
RewriteEngine on
RewriteRule index.php http://www.example.com/index.asp [R=301]
RewriteRule prods.html http://www.example.com/products.xhtml [R=301]
The first part ex, index.php is the local name index.asp is the remote name, [R=301] Means you are using a 301 as prescribed in RFC2616 meaning the file has moved permanently.
If all the files map perfectly in some way to another server you can do it like this:
RewriteEngine on
RewriteRule (.*) http://www.example.com/$1 [R=301]
Here you capture all requests and say that the file can now be found on example.com whatever it is called.
Best of luck.

or edit your index.php at your old site
<php
header('Location: http://newsite.com');
?>

Redirect http://oldsite.com http://newsite.com

Related

Redirecting from one page to another page on my website causes 404 error after using .htaccess file

Currently I'm working on a php blog.Where I have used .htaccess file to get clean url,here is the code.
RewriteEngine On
RewriteCond %{HTTPS} !=off
RewriteCond %{HTTP_HOST} ^mywebsitename\.com$
RewriteRule (.*) http://www.mywebsitename.com/$1 [R=301,L]
RewriteRule ^([0-9]+)/([a-zA-Z0-9_-]+)?$ post.php?post_id=$1&title=$2 [L]
RewriteRule ^(.+)/(admin|css|fonts|ico|include|js|images)/(.*)$ $2/$3 [L]
I got this code online.As I'm new to php and this htaccess thing,that's why I don't have much idea about it.
URL Before adding .htaccess file was:
[http://www.mywebsitename.com/post.php?post_id=12&title=post-title][1]
And,URL after adding .htaccess file:
http://www.mywebsitename.com/12/post-title
So rewriterule gave me the url that I want.But it has cause error in redirection of the page.Before using .htaccess file whenever i clicked on home button after visiting following url: http://www.mywebsitename.com/post.php?post_id=12&title=post-title
then URL redirected successfully to home page,that is:http://www.mywebsite.com/index.php page.
But now after using .htaccess file after visiting the same url it is redirecting like this:
http://www/mywebsitename.com/12/index.php
And now it is firing 404 page not found error.Because technically there is no such page present under 12 directory on server.
So after visiting the following link:http://www.mywebsitename.com/12/post-title
I want it to redirect properly as it was redirecting the pages before adding the .htaccess file.
Please guys help me out.
Thanks in advance.
Is the link http://www.mywebsitename.com/12/post-title your actual url. Is mywebsitename the your domain name? I think you should replace it with the actual domain name. It might just be a placeholder text

How do I route all subdomains to index.php using htaccess without 301 redirect

I would like to set up an htaccess file that routes requests for any subdomain to the index.php file in the /public_html/ folder. I have already configured the DNS to accept wildcard subdomains.
I plan to give users a personalised url for the site and pass the username via the subdomain, so it is important that the url remains and hence 301 redirects are out of the question.
My htaccess currently looks like this, but does not work (server not found):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule . /index.php?subdomain=%1 [L,QSA]
My end goal is to be able to get a url such as //joebloggs.mydomain.com/foo/bar to translate into something like //www.mydomain.com/index.php?user=joebloggs&route=/foo/bar.
What do I need to have in the htaccess file to make this work?
You can have it this way
# if request is for a subdomain (except "www")
RewriteCond %{HTTP_HOST} ^((?!www\.).+)\.mydomain\.com$ [NC]
# internally rewrite to index.php
RewriteRule ^((?!index\.php$).*)$ /index.php?subdomain=%1&route=$1 [L]
Since it seems the rule is executed when you're trying to navigate to the index.php, can't this be done with simple php logic within index.php?
I mightve misunderstood your goal, but it seems to me you could easily replace this with a GET. Your url would be ready for that approach, too.

Redirect subfolder to root domain

I want to redirect the subfolder and allcontents to root domain.
For example:
http://www.example.com/ubb/ will redirect to http://www.example.com
I put the .htaccess on ubb folder and the code is:
RewriteEngine On
RewriteRule ^.*$ / [R=302,L]
However when you access http://www.example.com/ubb/threads.php it will redirect to http://www.example.com/threads.php it should be http://www.example.com
Is there any code to try or other possible solution?
Place this rule in /ubb/.htaccess:
RewriteEngine On
RewriteBase /ubb/
RewriteRule ^ /? [R=302,L]
Also test it out in a different browser to avoid existing browser caches.
It's achievable using PHP which will be quiet easier I believe.
Create index.php inside ubb folder & add this code to it.
index.php
<?php
header('Location: http://www.example.com/');
?>
It'll redirect all request coming to www.example.com.

Redirecting index.php to domain root only without any GET variables

On my site www.apptic.me I used to have a redirect rule to always redirect index.php to the domain root.
I realized that this made the mobile layouts at the bottom here and here lose the "index.php" portion of the URL and PHP was no longer able to read the GET variables.
So I deleted the redirect. But now when users navigate to plain old www.apptic.me/index.php the "index.php" stays. I want to redirect index.php to the root if there are no GET variables.
Is this possible and how would I go about doing this?
Place this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\s [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
PHP can still read the $_GET variables even with redirection so there is no longer any problem with redirection.

Mode_rewrite adding index.php to end of url

I want to apologize for not being a strong knowledge of the language, but I have a problem like that.
I made a redirect through mode_rewrite now url is "localhost/url/url2/" apache and it redirects to
"localhost/url/url2/index.php" how to remove from url "index.php?"
if you want to remove only index.php then put code like this in htaccess,
RewriteRule ^/$ http://localhost/url/url2/index.php
place this htaccess file where index.php is
You can use this rule:
RewriteRule ^url/url2/$ /url/url2/index.php [L]
Browser won't show index.php in the URL input.

Categories