2017-10-06 4 views
-3

가 어떻게이 같은 URL을 다시 작성할 수 있습니다 :URL 리디렉션 URL 및 에코을 위해 (URL 라우팅?)를 사용

내용의 PHP로
example.com/page.php?link=Anythingelse to example.com/Anythingelse and using 


<h1><?php echo $link; ?></h1> 

이 :

출력 예 : 웹 사이트 -title : Anythingelse - 웹 사이트 - 제목과 머리글 1 Anythingelse

첫 번째 아이디어는 다음과 같습니다

RewriteEngine On 
# Redirect e.g. /page.php?link=value to /value if direct acccess 
RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteCond %{QUERY_STRING} ^link=([^&]+)$ 
RewriteRule ^/?page\.php$ /%1 [R=301,QSD,NE,L] 
# Internal rewrite to from /value to /page.php?link=value 
RewriteCond %{REQUEST_URI} !^/?page\.php$ 
RewriteRule ^/?([^/]+)$ /page.php?link=$1 [QSA,NE,L] 

하지만, 500 내부 서버 오류가 발생합니다. 내 공급자는 Apache 오류 로그를 지원하지 않습니다. 내가 무엇을 할 수 있을지? PHP로 URL 라우팅?

+0

[찾았습니다.] (https://www.addedbytes.com/blog/url-rewriting-for-beginners)가 있습니까? – Wndrr

+0

URL 재 작성에 대한 많은 질문, 자습서, 안내서 등이 있습니다. 먼저 조사를 해보고 시도하십시오. –

+0

[PHP로 URL 재 작성] 가능한 복제본 (https://stackoverflow.com/questions/16388959/url-rewriting-with-php) – Neodan

답변

1

나는 당신이 원하는 것을 알고 있다고 가정합니다.

과 같아야 브라우저에서 요청 :

http://example.com/Example%20in%20Example 

%20 서버 (변경 없음에 의해 다시 공백 문자

이 요청 내부해야한다의 URL 인코딩 된 버전입니다 브라우저)를

http://example.com/page.php?link=Example%20in%20Example 

으로 변경하려면 다음을 작성하십시오. CCESS 파일 : 당신의 page.php 파일에 지금

RewriteEngine On 
# Redirect e.g. /page.php?link=value to /value if direct acccess 
RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteCond %{QUERY_STRING} ^link=([^&]+)$ 
RewriteRule ^/?page\.php$ /%1 [R=301,QSD,NE,L] 
# Internal rewrite to from /value to /page.php?link=value 
RewriteCond %{REQUEST_URI} !^/?page\.php$ 
RewriteRule ^/?([^/]+)$ /page.php?link=$1 [QSA,NE,L] 

이 값 Example%20in%20Example 사용하려면 :

<?php 
    $link = (isset($_GET['link'])) ? htmlspecialchars(urldecode($_GET['link'])) : ''; 
?> 
<h1><?php echo $link; ?></h1>` 

urldecode() 모든 URL 인코딩 된 문자 인 것, 예를 HTML 문자가 URL에 사용하는 경우
http://php.net/manual/en/function.urldecode.php

htmlspecialchars() 당신을 보호, 예를 들면 : %20는 공간이된다 < 또는 >
http://php.net/manual/en/function.htmlspecialchars.php

당신은 아파치의 mod_rewrite를 활성화해야합니다 그리고 당신은 htaccess로 파일을 덮어을 사용할 수있는 권한이 필요합니다.

+0

답장을 보내 주셔서 감사합니다.하지만 이제는 http://example.com/page.php?link=Example%20in%20Example – Royalts

+0

을 표시하여 500 명의 내부 서버 오류가 발생했습니다. 한 줄만 깜빡 했으니 이제는 제대로 작동 할 것입니다. – Webdesigner

+0

hmpf가 작동하지 않고, URL에서 아무것도 변경되지 않습니다. 그러나 index.php는 페이지와 같은 제목을 변경합니다.php (현재 있습니다.)

index.php

Royalts