1

.htaccess or php redirect to url friendly when index.php? * = * Is directly acce...

 2 years ago
source link: https://www.codesd.com/item/htaccess-or-php-redirect-to-url-friendly-when-index-php-is-directly-accessible.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

.htaccess or php redirect to url friendly when index.php? * = * Is directly accessible

advertisements

i am working on a new design for my gaming site, and what i have done is made a rewrite rule:

RewriteRule ^play/([^/\.]+)/?$ index.php?play=$1 [L]

this works perfectly, when a user opens /play/gamename that stays in the address bar (canonical url) and it loads the game page using my index.php.

however, what i want to do now, is that when a user access the old url (index.php?play=gamename) they should get redirected to the new canonical url (/play/gamename).

could someone please type up the code for me to do this? i am open to both doing this with the .htaccess file or the index.php file, whichever works best.

another problem is that i already have a lot of facebook comments and likes, which refer to the index.php?play=gamename pages, would it also be possible to somehow move these likes and comments to the new prettier urls?


I think you'll be looking for something like this to redirect from your old URL to your new URL while also maintaining that URL as the actual place the script is served from.

Options +FollowSymlinks
RewriteEngine On

RewriteBase /

# Stop rewrite from doing an infinite loop by rejecting redirects
RewriteCond %{ENV:REDIRECT_STATUS} !=200
RewriteCond %{REQUEST_URI}  ^/index\.php$
RewriteCond %{QUERY_STRING} ^play=(.*)$
RewriteRule .* /play/%1? [NC,R=301,L]

# Rewrite all requests from /play/ to index.php
RewriteRule ^play/(.*)$ /index.php?play=$1 [NC,L]

As for facebook, there is nothing you can do about old comments, but your rewrite will take care of that for any future clicks through to your site.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK