0

I have just one index.php file that generates the other pages using:

RewriteRule ^page 1.php$ index.php?cat=a [NC]
RewriteRule ^page 2.php$ index.php?cat=b [NC]

I'm trying to use the following code to change the title of my pages but it's working just for index.php.

<?php
$page = $_SERVER['PHP_SELF'];
if(isset($page)) {
switch($page) {
case "/index.php":
$title = "this is homepage";
break;
case "/page 1.php":
$title = "products";
break;
case "/page 2.php":
$title = "services";
break;
}
}else{
$title = "default title";
}

print "<title>$title</title>";
?>
4
  • Did you echoed $page before? Perhaps the whitespace was replaced with %20. Commented Jul 30, 2013 at 11:04
  • PHP sees the rewritten URL, not the original. You need to test $_GET['cat']. Commented Jul 30, 2013 at 11:04
  • @Barmar I've tested $_Get... It's not it. Commented Jul 31, 2013 at 6:26
  • Why do people keep writing PHP like this? Commented Aug 1, 2013 at 8:05

2 Answers 2

1

verify the spaces I'm not sure they allowed in urls

Have a look to the documentation :

http://php.net/manual/en/reserved.variables.server.php

try this :

<?php
$page = $_GET['cat'];
if(isset($page)) {
switch($page) {
case "index":
$title = "this is homepage";
break;
case "a":
$title = "products";
break;
case "b":
$title = "services";
break;
}
}else{
$title = "default title";
}

print "<title>$title</title>";
?>
Sign up to request clarification or add additional context in comments.

7 Comments

my htaccess it's fine, I use "-"... it was just an example. The code for changing the title is not working for pages
@OlivierSoulet now i use this, so it should take only the page i need but still is not working: $path=$_SERVER['PHP_SELF']; $page=basename($path); if(isset($page)) { switch($page)....
have you tried to display what is in $_SERVER['PHP_SELF'] ? Try a var_dump of this var
@OlivierSoulet you mean "var_dump($_SERVER)" ? ... i get a lot of things like: ["DOCUMENT_ROOT"]=> string(33) "/home/dany/public_html/mysite.com" ... ["QUERY_STRING"]=> string(11) "cat=contact" ["REDIRECT_QUERY_STRING"]=> string(11) "cat=contact" ..... ["REDIRECT_URL"]=> string(12) "/contact.php" ....["REQUEST_URI"]=> string(12) "/contact.php" ["SCRIPT_FILENAME"]=> string(43) "/home/dany/public_html/mysite.com/index.php" ... ["PHP_SELF"]=> string(10) "/index.php" ["REQUEST_TIME"]=> int(1375259963) ["argv"]=> array(1) { [0]=> string(11) "cat=contact" } ["argc"]=> int(1) } ?>
@Freelancer you mean "var_dump($_SERVER)" ? ... i get a lot of things like: ["DOCUMENT_ROOT"]=> string(33) "/home/dany/public_html/mysite.com" ... ["QUERY_STRING"]=> string(11) "cat=contact" ["REDIRECT_QUERY_STRING"]=> string(11) "cat=contact" ..... ["REDIRECT_URL"]=> string(12) "/contact.php" ....["REQUEST_URI"]=> string(12) "/contact.php" ["SCRIPT_FILENAME"]=> string(43) "/home/dany/public_html/mysite.com/index.php" ... ["PHP_SELF"]=> string(10) "/index.php"
|
0

Use \s in you rewrite rule instead of space:

RewriteRule ^page\s1.php$ index.php?cat=a [NC]
RewriteRule ^page\s2.php$ index.php?cat=b [NC]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.