내 사이트에 친숙한 URL을 만들고 싶습니다. 이것이 내 코드입니다.
이 코드의 문제는 우리에 대해 링크를 클릭하면 URL이 localhost / php_friendly_url / about / 12가되었지만 기본 링크를 클릭하면 URL 이이 localhost / php_friendly_url / about / main처럼 바뀌 었습니다. 내 코드
내 문제를 도와주세요.
main.php
<html>
<body>
<h1>Main Page</h1>
<ul>
<li><a href="main">Main</a></li>
<li><a href="about/12">About US</a></li>
</ul>
</body>
</html>
index.php
<?php
include 'class/friendlyUrl.php';
$url = new friendlyUrl('/php_friendly_url');
if(!$url->segment(1))
$page = 'main';
else
$page = $url->segment(1);
switch($page) {
case 'main':
include 'main.php';
break;
case 'about':
include 'about.php';
break;
default:
echo '404 not found';
break;
}
?>
friendlyUrl.php
<?php
class {
var $site_path;
function __construct($site_path){
$this->site_path = $this->removeSlash($site_path);
}
function __toString(){
return $this->site_path;
}
private function removeSlash($string){
if( $string[strlen($string) - 1 ] == '/' )
$string = rtrim($string, '/');
return $string;
}
function segment($segment){
$url = str_replace($this->site_path, '', $_SERVER['REQUEST_URI']);
$url = explode('/', $url);
if( isset($url[$segment]) ){
return $url[$segment];
}
else
return false;
return $url;
}
}
?>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
</IfModule>
다음과 같이 URL 시작 부분에 "/"를 작성하기 만하면됩니다.
<ul>
<li><a href="/main">Main</a></li>
<li><a href="/about/12">About US</a></li>
</ul>
"특별한"PHP 코드가 필요하지 않습니다.)
귀하의 웹 사이트가 루트 폴더가 아니라 "test"하위 폴더에 있다고 말하면 일반적으로 다음과 같은 "config"파일 (모든 파일에 포함)에 상수를 생성합니다.
define( 'URL', 'http://mywebdomain.com/test' );
그런 다음 모든 링크에서이 상수를 사용합니다.
<ul>
<li><a href="<?php echo URL ?>/main">Main</a></li>
<li><a href="<?php echo URL ?>/about/12">About US</a></li>
</ul>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다