-3

How can I rewrite a URL like this:

example.com/page.php?link=Anythingelse to example.com/Anythingelse and using 


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

this with php in content:

example output: Website-Title: Anythingelse - Website-Title and Heading1 Anythingelse

First Idea is:

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]

but it makes a 500 internal server error. My Provider doesnt support Apache error logs. What can i do? URL Routing with PHP?

Royalts
  • 1
  • 2
  • Have you [looked it up](https://www.addedbytes.com/blog/url-rewriting-for-beginners) ? – Mathieu VIALES Oct 06 '17 at 12:56
  • There are many questions, tutorials, guides, etc about URL rewriting. Please do some research first and make an attempt. – Patrick Q Oct 06 '17 at 12:57
  • Possible duplicate of [URL rewriting with PHP](https://stackoverflow.com/questions/16388959/url-rewriting-with-php) – Neodan Oct 06 '17 at 13:03
  • Thanks for Help @all. Ive read so many but nothing worked for me :-( . – Royalts Oct 06 '17 at 13:38
  • Hello on Stack Overflow. I would suggest that you rephrase your question. Show us what you have so far. Point out where you stuck at and what worked already. check also https://stackoverflow.com/help/how-to-ask – Webdesigner Oct 06 '17 at 15:40
  • If you read a bunch and tried a bunch you should show us those attempts and explain what didn't work about each one and what your exact confusion was with each one. – takendarkk Oct 06 '17 at 18:07

1 Answers1

1

I assume that I know what you want.

The request from the Browser should look like this:

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

%20 is the URL encoded version of a space character

This request should be internal rewritten by the server (no change in the Browser) to

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

To do this rewrite put the following into your htaccess file:

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]

Now in your page.php file you want to use this value Example%20in%20Example:

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

urldecode() will decode all URL encoded character, e.g. %20 becomes a space:
http://php.net/manual/en/function.urldecode.php

htmlspecialchars() protect you if HTML character are use in your URL, e.g. < or >
http://php.net/manual/en/function.htmlspecialchars.php

You need to have apache mod_rewrite enabled and you need the privilege to use Override with htaccess files.

Webdesigner
  • 1,954
  • 1
  • 9
  • 16
  • Hello and very special thanks for an answer. But now comes an 500 internal server error by showing http://example.com/page.php?link=Example%20in%20Example – Royalts Oct 06 '17 at 16:49
  • hmpf not working, nothing change in the url. But the index.php is also change the title like page.php (now is there

    index.php

    .
    – Royalts Oct 06 '17 at 17:24
  • What url did you enter? Do you some other Rewrite Rules? – Webdesigner Oct 06 '17 at 17:27
  • aah sorry, you understand me and i you. I would to rewrite example.com/page.php?link=Anythingelse to example.com/Anythingelse and use it for Content

    Anythingelse

    – Royalts Oct 06 '17 at 17:32
  • But this is not rewrite this is redirect see https://stackoverflow.com/a/46595140/5427950 But I asume that internaly `example.com/page.php?link=Anythingelse` should be called – Webdesigner Oct 06 '17 at 17:33
  • yes when i redirect it then link=Anythingelse not working – Royalts Oct 06 '17 at 17:38
  • Did add the redirect ... NEXT TIME PLEASE WRITE YOU QUESTION MORE PRECISE – Webdesigner Oct 06 '17 at 18:04
  • ok sorry, my provider answer these: "We don't have apache logs so it is not possible to provide one. We think that line 5 is problematic here: RewriteCond %{REQUEST_URI} !^/?page\.php$ Also, it is not an approved answer on stackoverflow, it starts with "I assume that I know what you want" and no one has validated the syntaxes. – Royalts Oct 06 '17 at 20:05
  • and this: As I check, the .htaccess needs to be revised. Sorry, we don't have the logs being asked by the person assisting you. – Royalts Oct 06 '17 at 20:09
  • The point is your Question is not very well written. I'm the only one that try to help you. No one else will do it as long es you do not provide more infos and write a popper question. And `RewriteCond %{REQUEST_URI} !^/?page\.php$` is required or you have a rewrite loop. – Webdesigner Oct 06 '17 at 20:12
  • And how schould i rewrite the question here. My english is not so good :-( – Royalts Oct 06 '17 at 20:43