hello stackoverflow community hopefully you understand what im trying to do
im working with pretty URLs with htaccess and php and i have seen in some pages that in the URL appear the title of the news something like...
http://www.samplePage.com/news/killer-shooter-in-new-york
i would like to know how i do that , im not that good in htaccess or friendly URLs but i try to do my best and this is the way that im doing it....
my .htaccess
Options All -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9/]+)$ index.php?view=$1
my html
//after the header label i add this to include the content
<?php
if (isset($_GET["view"])) {
$view=explode("/",$_GET["view"]);
include"modules/".$view[0]."-view.php";
}else{
include"modules/home-view.php";
}
?>
now i got these links that take me to the news
<ul class="row news-list">
<?php $sql=$con->query("SELECT * FROM latest_news LIMIT 4");
while ($row=$sql->fetch_array()) { ?>
//note: the URLSERVER is the route for example http://localhost/projectNews/
<li class="grid_6">
<?php echo $row['post_title']?>
</li>
<?php } ?>
</ul>
the ouput of this is http://localhost/projectNews/news/4/
once i click the link what i would like to do is to add the title of the new after the id of the news like i explained before for example...
http://localhost/project/news/4/killer-shooter-in-new-york
can someone of you guys help me please? im a little bit of new on this, thanks!
Related
I am trying to open a page on clicking on a link in my codeigniter application. This is the snippet of my attempt
at the controller level this is snippet
//load return policy
function returnpolicy()
{
$this->load->view("return_policy");
}
at the view level this is the snippet
<div class="toggle">
Return Policy
</div>
on clicking on the link it does not navigate to the page. please what am I missing. am new to codeigniter
You should understand the difference between site_url and base_url first
echo base_url(); // http://example.com/website
echo site_url(); // http://example.com/website/index.php
You can rewrite it as follows :
<?php echo site_url('controller/method');?>
In HMVC
<?php echo site_url('module/controller/method');?>
So in your case
Return Policy
you can use the URL helper this way to generate an <a> tag
anchor(uri segments, text, attributes)
So... to use it...
<?php echo anchor('gotopolicy/returnpolicy', 'Return Policy', 'class="toggle-title"') ?>
Possible issue could be of htaccess,
create a file .htaccess in your root CI folder
and add below contents to it
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Make $config['index_page'] = "";
try to remove class="toggle-title"
or try this
<div >
Return Policy
</div>
I have a menu like:
<ul>
<li>Home</li>
<li>Contect</li>
<li>References</li>
</ul>
And i would like to tell that the / use home.php, the /contact get method use the contact.php and the /references use the references.php and of course th active class also change when we are at the contact menupoint and so on....
Thanks for the help! Im new with php sorr..
Easiest way to do it:
create a file with a name .htaccess
RewriteEngine on
RewriteRule ^/([^/\.]+)/?$ index.php?page=$1 [L]
if your files are under another folder like `includes/contact.php``
RewriteEngine on
RewriteRule ^/includes/([^/\.]+)/?$ index.php?page=$1 [L]
getting your page via 'GET' in your index.php
$page = $_GET['page']; //contact.php
and test:
Contact
I have a little problem I can't seem to get my head around. What I'm trying to do is that when someone "http://domain.com/directory/ImgVariableHere" it will display the image on a php page. I know I'm probably going about this the complete wrong way and that's why I've come here. I'm a newbie to PHP so please forgive me. Here's the code I have.
<?php
$img="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo "<img src=\"$img.png\">\n";
?>
Here's the code it echos.
<img src="http://domain.com/directory/.png">
I'm probably explaining this really badly, ask any questions of you need to. Thanks. If you have a completely different and better way of doing it then please do tell.
EDIT: What I'm trying to do is kind of like Gyazo. They show their images on a webpage, I'm trying to do that but by using variables in PHP and I just can't wrap my head around it.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [PT,L]
</IfModule>
Put this in .htaccess in the root of your document, and it should route all requests to index.php, then you just put the script above in index.php, and it should work.
It looks like you want to have a RewriteURL with a variable in it.
So when you want this we can create a .htaccess which will reserve a spot for your variable. Example is like this:
RewriteEngine on
RewriteBase / #Maybe your in a subfolder? Change this to /yoursubfolder/secondsub/...
#Redirect all urls that begin with /image/ to image_shower.php?image=<image value>
RewriteRule image/(.*) image_shower.php?image=$1
This rewrite all the urls that begin with /image/ to the image_shower.php without showing the image_shower.php URL. It also sends the information in a GET variable. Which can be easily retrieve in PHP. Like this:
<?php
$img="http://".$_SERVER['HTTP_HOST']. '/' . $_GET['image'];
echo "<img src=\"$img.png\">\n";
?>
Hope this will help you go further!
I had an idea of writing a page template in my index.php where the content section of the page will include the content dynamically by determining what url the browser is at and getting the content page based on that. There's a few issues though:
<?php
// /index.php
?>
<!-- html tags and menu/layout divs as needed go here -->
<section id="content">
<?php
function curPageURL()
{
$page = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
return $page;
}
$page = curPageURL();
switch ($page)
{
case "index.php":
include_once("includes/home.php");
break;
}
?>
</section>
<!-- footer and such goes here -->
Although this works, I'm having trouble applying the concept further. I just can't seem to get my head around how the navigation should work or how to apply this concept to other pages in my site...
If anyone can advise, I'd really appreciate it.
You could use a .htaccess file (put in the same directory as index.php).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?uri=$1 [L,QSA]
And then in index.php you can get the page with:
$page = $_GET['uri'];
So if you user goes to example.com/home then the page variable will be set to home.
hello i have an anchor on my site. by calling my site the url will be:
http://site.com/page.php
by clicking a button the url will display an url that i would like to hide:
http://site.com/page.php#1
now i have found out that i can change this by using mod_rewrite tool. i tried this rule without success:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)\.html$
RewriteRule ^.*\.html$ %1.php [L]
for calling the page as html or php and to hide the #1 behind the .php
RewriteRule ^(.*)$ ./php#1=php$1
i really would appreciate if there is someone who could give me advise on how to solve this. thanks a lot.
You cannot achieve this via mod_rewrite.
URL after # are not passed through server.
If you still want to achieve this, you may want to look into doing it via JavaScript:
Check this out
JavaScript:
function scrollHere() {
// Scroll to the anchor
//(you may need to look for some script to achieve this)
return false; // to prevent URL overwriting
}
One such script is this jQuery.scrollTo.
The hashtag is never sent to the server, thus you simply cannot create a rule, that based on it.
You can do something like this to remove # tag.
Your text.
So when you write "javascript:void(0);" in href attribute of a tag and thats it.
HTML
<a href='page'>Home Page</a>
<a href='page/1'>Page 1</a>
<a href='page/2'>Page 2</a>
<a href='page/3'>Page 3</a>
Rewriting as
RewriteRule ^$ page.php [L]
RewriteRule ^([a-z]+)/?([0-9]*)/?$ page.php?index=$2 [L]
And check for index in php
<?php
if (isset($_GET['index']))
{
$pageNo =$_GET['index'];
}
//based on page NO display contents
?>