Forum Moderators: phranque
http://example.com/index.php?page=about
http://example.com/about
index.php?page=to
http://example.com/$GETvariable name.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1
<li><a href="index.php?page=about">About</a></li>
http://example.com/index.php?page=about
http://example.com/about
[edited by: phranque at 2:24 pm (utc) on Jul 23, 2013]
[edit reason] Please Use Example.com [webmasterworld.com] [/edit]
http://localhost/index.php?page=about displays the correct content when the redirecting rule and it's conditions are all commented out (add a # to the start of each line), then the PHP script is working fine. http://localhost/about and at this point it doesn't matter whether the redirecting rule and its conditions are commented out or not, but the rewriting rule must NOT be commented out. http://localhost/index.php?page=about (or http://localhost/foldername/index.php?page=about if there is a folder involved) should redirect to the new format URL and then show the correct content.
<?php
if ($_GET['page'] == ''){
$pg='home';
}
else {
$pg=($_GET['page']);
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test website</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="wrapper">
<div id="header"><h1>Header</h1></div>
<div id="nav_main">
<ul>
<li><a href="index.php?page=home">Home</a></li>
<li><a href="index.php?page=about">About</a></li>
<li><a href="index.php?page=contact">Contact</a></li>
</ul>
</div>
<div id="content">
<div id="content_right"></div>
<div id="content_main"><?php include('content/'.$pg.'.php') ?> </div>
</div>
<div id="footer">Footer</div>
</div>
</body>
</html>
if ($_GET['page'] == ''){
$pg='home';
}
else {
$pg=($_GET['page']);
}
<div id="content_main"><?php include('content/'.$pg.'.php') ?> </div>
<a href="index.php?page=about"> to <a href="/about"> ?
RewriteRule /about widgets.html [L]
RewriteRule /about widgets.html [L]
http://localhost/widgets.html RewriteRule ^about$ /widgets.html [L] http://localhost/about RewriteCond %{THE_REQUEST} [A-Z]{3,9}\ /widgets\.html\ HTTP/
RewriteRule ^widgets\.html$ http://localhost/about [R=301,L] http://localhost/about URL. This is a redirect. RewriteCond looking at %{THE_REQUEST} prevents an infinite loop. In this case it looks again at the requested path. [edited by: phranque at 5:25 pm (utc) on Jul 30, 2013]
[edit reason] unlinked url [/edit]
then your rewriting rule should be
RewriteRule ^about$ /widgets.html [L]
if ($_GET['page'] == ''){
$pg='home';
}
else {
$pg=($_GET['page']);
}
if (empty($_GET['page'])) <html>
<head>
<title>The Widget Page</title>
</head>
<body>
This is a fake page!
</body>
</html
and use that. (It does not have to validate, your browser just has to be able to open it ;))
. (a dot) matches any single character, except the ending of a line.
\ is called an escaping character, this removes the function from a 'special character' (EG if you needed to match index.php?, which has both a . (dot) and a ?, you would have to 'escape' the special characters . (dot) and ? with a \ to remove their 'special' value it looks like this: index\.php\?)
I must admit that I thought that rewriting would be easier.
index.php index.php
indexaphp
index/php
index3php
[edited by: lucy24 at 12:12 am (utc) on Jul 28, 2013]
RewriteCond %{THE_REQUEST} \? THE_REQUEST contains a literal question mark. THE_REQUEST is the literal HTTP request sent by the browser: GET / HTTP/1.1 GET /about HTTP/1.1 GET /about.html HTTP/1.1 GET /index.php HTTP/1.1 GET /index.php?page=about HTTP/1.1 [edited by: g1smd at 12:23 am (utc) on Jul 28, 2013]
[edited by: phranque at 5:27 pm (utc) on Jul 30, 2013]
[edit reason] unlinked urls [/edit]
When I type it manualy localhost/root_folder/widgets.html it works well
localhost:1337/about
Connection failed
Firefox can't establish connection with server
There's another thing you can do on your local WAMP. First shut down the server and make a copy of the config file. Now EDIT the config file-- the real one, not the copy-- to add a RewriteLog:
RewriteLog /logs/rewrite.log
RewriteLogLevel 9
Restart the server and continue testing. But if mod_rewrite already makes you anxious, logging rewrites may not make you any happier.
[edited by: phranque at 5:29 pm (utc) on Jul 30, 2013]
[edit reason] unlinked url [/edit]
I had that problem that only when I write in the code http://localhost/blalala
[edited by: lucy24 at 10:09 pm (utc) on Jul 28, 2013]
RewriteEngine on
RewriteRule about /testing/widgets.html [R=301,L]
Now on address bar it writes normal localhost without localhost:1337 or so.
RewriteEngine on
RewriteBase /
RewriteRule about /widgets.php [R=301,L]
[edited by: phranque at 5:24 pm (utc) on Jul 30, 2013]
[edit reason] no personal urls please [/edit]
RewriteEngine on
RewriteBase /(this is only for on-line server testing)
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} page=([a-z]+)
RewriteRule ^index\.php$ /%1? [R=301,L]
RewriteRule ^([a-z]+)$ /index.php?page=$1 [L]
Options +FollowSymLinks