简体   繁体   中英

301 redirect with .htaccess in dynamic PHP website

I wanted to ask you a question about 301 URL redirection. So, I have a website and it's former URL used to be:

http://www.sample.com/tutorials.php?name=sample

But, now it's:

http://www.sample.com/tutorials/programming/sample.php

So, you can conclude that the syntax of the new URL is this:

http://www.sample.com/tutorials/(name of the category)/(value of the variable).php

I've been toying around with 301 redirect, but I got 500 Internal server error when I used this code:

RewriteEngine On
RewriteCond %{COND ^tutorials.php?name=sample$ HTTP/1.1} ^[A-Z]{3,9}\ /tutorials\.php\?name=([^&\ ]+)
RewriteRule ^ /tutorials/programming/%1.php [L,R=301]

I have also tried to create my own 301 redirect code, which gives me no 500 Internal server error, but I don't know will it work (I can't test it on localhost, because search engines don't index localhost). Here it is :

RewriteEngine On
Redirect 301 /tutorials.php?name=$1 http://www.sample.com/tutorials/programming/(.+)\.php

So, if my code above is good than great, but if it's not, can someone please explain (as slowly and as detailed as possible, please) how to 301 redirect my old site to my new one. As I mentioned, I use dynamic web-sites and I am rewritting using .htaccess file (mod_rewrite).

I have tried to be as detailed as possible. If you need additional details, feel free to ask.

You have to redirect the old links to your new domain. This is how you can do it.

Using htaccess :

Create a .htaccess file with the below code, it 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 (ie the same directory where your index file is placed)

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L] 

In addition to the redirect I would suggest that you contact every backlinking site to modify their backlink to point to your new website.

Note: This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

Refer this for more methods

EDIT:

Create a .htaccess file with the below code, it will ensure that all requests coming in to domain.com will get redirected to www.domain.com The .htaccess file needs to be placed in the root directory of your old website (ie the same directory where your index file is placed)

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM