Solved!
first of all, I know I can use "?page=..." but I do not like that variable in my URL. Now after some searching I found a site which uses the $_GET['page'] without showing the "?page=" in the url.
But I'm having a bit of a struggle with the code. I cannot seem to get this code to work. I know it works on an other site, but not on my site. I cannot seem to find how it works on the other site.
this is the menu code:
<li>Home</li>
<li>Even voorstellen</li>
<li>De kennismaking</li>
and this is the php code:
$sExpressie = "(http:|ftp:|shttp:|www.|.php|.pl|.cgi|.asp|index.php)";
if(isset($_GET['pagina']) && eregi($sExpressie,$_GET['pagina']))
{
include("pages/home.php");
}
else
{
if(isset($_GET['pagina']) && file_exists('Pages/' . $_GET['pagina'] . ".php"))
{
include('pages/' . $_GET['pagina'] . ".php");
}
else
{
include("pages/home.php");
}
}
I don't understand how this code works. In my website the "$GET['pagina']" is never filled, which is logical. But how can it be filled in the other website?
Thanks a lot everyone!
Solution:
Add the following to your .htaccess file:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ pages/$1.php [NC,L]
You'll need to add the following to your .htaccess file:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ pages/$1.php [NC,L]
For example, if you now navigate to http://yourwebsite.com/fubar, your website will load the page located at pages/fubar.php without changing the URL.
You can use a RewriteRule within your .htaccess file.
For example:
RewriteEngine On
RewriteRule ^/(.*)$ index.php?pagina=$1 [L, QSA] #(.*) is lazy and you may want to just catch [a-z0-9].
This will "rewrite" a request like http://example.com/?pagina=even_voorstellen into something like http://example.com/even_voorstellen. Your index.php can then use $_GET['pagina'] to call the respective page.
Related
Hi I am trying to change my url using htaccess but it didn't work anymore.
http://localhost:8888/cPanel/abc?page=general-settings
RewriteRule ^cPanel/([\w-]+)/?$ abc.php?page=$1 [L,QSA]
What i am doing wrong anyone can help me here please ?
I want to change the url like this:
http://localhost:8888/cPanel/general-settings
<?php
$page ='';
if($_GET['page']){
$page = $_GET['page'];
if($page == 'general-settings'){
include "/pages/general-settings.php" ;
}
}
?>
The error stands in the rule. Your actual rule is:
RewriteRule ^cPanel/([\w-]+)/?$ abc.php?page=$1 [L,QSA]
which is missing cPanel, if you want to achieve http://localhost:8888/cPanel/general-settings as result
With this rule (which means: when you Apache match cPanel/*anything*, hit the resource at cPanel/abc.php?page=*anything*) it should work:
RewriteRule ^cPanel/(.*)$ cPanel/abc.php?page=$1 [L,QSA]
Test this code
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^cPanel/(.+)/?$ abc.php?page=$1
url: http://localhost:8888/cPanel/general-settings
I know that similar questions were already asked, but i could not find any information for my specific "problem".
What i want to do is the following in a very dynamic way, which means that a "SuperUser" should be able to define new routes in a admin interface:
If a user enters http://www.example.com/nice-url/
he should get redirected to http://www.example.com/category.php?id=123 without changing the url.
Now there are a few ways i can achieve this. Either i use .htaccess like this:
RewriteEngine on
RewriteRule ^nice-url category.php?id=123 [L]
This works, but is not very dynamic. I would need to let a php script append new rules at the bottom which is not something i would like to do.
Or this:
.htaccess
FallbackResource /process.php
process.php
$path = ltrim($_SERVER['REQUEST_URI'], '/');
$elements = explode('/', $path);
if(count($elements) == 0) {
header("Location: http://www.example.com");
exit;
}
$sql = sprintf("SELECT Target FROM urlrewrites WHERE Source = '%s' LIMIT 1", array_shift($elements));
$result = execQuery($sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$target = $row['Target'];
header("Location: ".$target);
exit;
This also works, but sadly the url in the address bar gets changed. Is there a way in the middle of both? Having the flexibilty of PHP and the "silentness" of RewriteEngine? How do great CMS like joomla do it? Do they generate a htaccess file for each new page you create?
Thanks!
You can have this code in root .htaccess:
RewriteEngine on
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ category.php?uri=$1 [L,QSA]
PS: Note this will route /nice-url to /category.php?uri=nice-url. Then inside category.php you can dip into your database and translate $_GET['uri'] to id.
It would be more dynamic if you use regular expressions. That's how it works in CMS systems like Joomla. Good idea is to install Joomla with 'nice' URLs on and look over .htacces file to get to know how does it work.
You can start here:
http://www.elated.com/articles/mod-rewrite-tutorial-for-absolute-beginners/
Sample Code
Here is a sample of my .htaccess file:
RewriteEngine On
RewriteRule ^home/?$ index.php?intro=true [L]
RewriteRule ^home/([^/]*)$ index.php?location=$1&intro=true [L]
RewriteRule ^wedding/?$ wedding.php [L]
RewriteRule ^wedding/([^/]*)$ wedding.php?location=$1 [L]
And here is some sample code that is featured on both the index.php and wedding.php page:
index.php:
if(!$_GET["location"]) { $location = "London"; } else { $location = ucwords($_GET["location"]); }
[....]
<h1>Ben Pearl, <?php echo $location; ?> Magician</h1>
wedding.php:
if(!$_GET["location"]) { $location = "London"; } else { $location = ucwords($_GET["location"]); }
....
<h1><?php echo $location; ?> Wedding Magician</h1>
What is supposed to happen
The $location string should be effected by the $_GET value 'location'.
What is happening
The rewrite is working fine on index.php; if a user goes to example.com/home/place, $location is replaced by place.
However, on every other page (including the page with script pasted above), the string is replaced by "london", implying that the page hasn't received the $_GET data and the rewrite rule is not working correctly.
What's stranger is that the exact same code, unaltered, worked fine on my localhost.
Try turning off Multiviews, which turns on mod_negotiation's "fuzzy" request URI to file mapping. When mod_negotiation sees /wedding/ and then it sees that there's a file /wedding.php, it'll kick in and send the request directly there, completely bypassing mod_rewrite and your rules.
On top of your htaccess file, add:
Options -Multiviews
That may also explain why it works for the rewrite to index.php, since /home doesn't look much like /index.php (whereas if you had a home.php, mod_negotiation would try to map to that instead).
I want:
sub.domain.com to load index.php
sub.domain.com?s=custom to load /custom/index.php
I'm doing this right now:
In .htaccess:
RewriteRule ^(custom)/?$ index.php?subject=$1
and in index.php I have this:
if($_GET['s'] == 'custom') {
header( 'Location: http://sub.domain.com/custom/index.php' ) ;
}
... but is it possible to do the redirect via htaccess itself depending on the GET variable?
Thanks!
Yes, you could check query string with RewriteCond
RewriteCond %{QUERY_STRING} s=custom
RewriteRule ^(.*?)$ custom/index.php [L]
With this, it redirects all requests with existing "custom" get parameter to index.php, and this can be extended :)
Idea: http://statichtml.com/2010/mod-rewrite-baseon-on-query-string.html
You probably want to use "RedirectMatch"
RedirectMatch ^sub.domain.com?s=custom$ /custom/index.php
Maybe youll need to play with the regex abit
So what I am trying to do is create a website where the index.php will piece all the pages together with based upon parameters sent by query string.
I want to "rewrite" the urls from the form: http://example.com/index.php?p=cat1/page1 to something like http://example.com/cat1/page1
I am attempting to make use of .htaccess mod_rewrite rules, but have been unable to make anything work.
I know there are mountains of info on this stuff.. I have spent a few hours already reading about it and tinkering, but I feel stuck. Nothing I am doing seems to yield any results.
Mod_rewrite IS loaded on the server (says phpinfo())
My .htaccess code:
RewriteEngine On
RewriteRule ^/([home|cat1|cat2]/)?([a-zA-Z]+_?)*/?$ /index.php?p=$1/$2 [L]
My index.php code:
<?php
if (isset($_GET["p"])){
$page = htmlspecialchars($_GET["p"]);
}else{
$page = "home";
}
include('./template/header.php');
if (is_dir("./pages/$page")){
include("./pages/$page/overview.php");
}else{
if (file_exists("./pages/$page.php")){
include("./pages/$page.php");
}else{
//not found
echo '<h1 id="pageNotFound">Page Not Found</h1>';
}
}
include('./template/footer.php');
?>
Here's your .htaccess fix:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !\.(jpg|jpeg|gif|png|css|js|pl|txt)$
RewriteRule ^(home|services)/([a-zA-Z_\-]+)(\/?)$ /NEP2/index.php?p=$1/$2 [NC,QSA,L]
About the PHP, can you move the pages below the public_html folder?