I have an website on my localserver (with Apace) and i want to
rewrite urls.
I have .htaccess file.
I have RewriteEngine ON (on Apache)
index.php
<h4><?php echo $row['postTitle']; ?></h4>
When someone press on the title of post will be redirected to post.php/post=postID
I want to rewrite this url (localhost/post.php/post=postID) to something like that:
/localhost/post/postTitle or /localhost/post/postID
Preferably the first one.
How can I do that?
.htaccess
RewriteEngine On
RewriteRule ^posts/([0-9]+)/?$ post.php?post=$1 [NC,L]
Nothing happens, when I press on the link, the url is the same (post.php/post=postID).
It is a manual process to update your links. So, you'll want to change:
<a href="post.php?post=<?php echo $row['postID'];?>">
<h4><?php echo $row['postTitle']; ?></h4>
</a>
to:
<a href="posts/<?php echo $row['postID'];?>">
<h4><?php echo $row['postTitle']; ?></h4>
</a>
Update: As a note, people like to use the shorthand for just echo'ing content, E.g.
<?=$row['postID']?>
So it would become:
<a href="posts/<?=$row['postID']?>">
<h4><?=$row['postTitle']?></h4>
</a>
Related
I got this php code form my Wordpress site, I works as expected on my localhost, but not when I moved it to live:
<li>
<a href="<?php echo get_site_url(); ?>#<?php echo $menu_id ?>">
<?php echo $menu_item->title ?>
</a>
</li>
On my localhost the url produced from the the code above looks like this:
http://localhost/wordpress/#home
But when moving to live, the exact same php code produces this url:
http://www.example.com/wordpress/#http://www.example.com/wordpress/#home
My site url field in wp_options table looks like this:
http://www.example.com/wordpress/
Why is the URL repeated twice when the site is live?
Check your wp-config.php weather WP_SITEURL is defined or not, if not then there is workaround to fix this issue by defining WP_SITEURL constant in wp-config.php like define('WP_SITEURL', 'http://www.example.com/wordpress/');
For example,
the user types in,
www.foob.com/some_path
I don't want to actually have a some_path folder for every different identifier the user might use ... for example if it is a person's name, as seen in many social apps.
www.foob.com/some_path1
However, i would like to read it in as a variable and serve the relevant content
JavaScript
var foo = 'some_path1'
PHP
$foo = 'some_path1';
How do I get 'some_path1' into foo?
Note
some_path is a variable, it might contain
some_path_1
some_path_2
some_path_3
Clarification
So take facebook.com
if you go to
www.facebook.com/your_name
it's not like there is a different directory for all 1 billion users.
A generic page is served populated with user specific data.
How do I get the id your_name?
You need to setup www.foob.com/some_path to point to a PHP script URL with index.php?q=some_path or similar in your .htaccess file:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-z\-]+)$ index.php?q=$1
Then it's just a matter of serving different content based on $_GET['q'] in the PHP file.
or to use it in Javascript:
echo '<script>...var q = ' . $_GET['q'] . ';</script>';
You can use $_SERVER['REQUEST_URI'] to get this information out with PHP. If you want it in JavaScript, take a look at the window.location object (probably window.location.href or window.location.pathname). Both might require a bit of parsing to only grab the part that you want, though.
Regarding the rewrite rule to send these requests to your script, I'd use something like this, which will redirect all requests for files that aren't found elsewhere through app.php. This should go in an .htaccess file on the root of your site.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
Add a .htaccess file to your root directory.
.htaccess
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?page=$1 [L]
You can add something similar to this at the top of your index.php page:
<html>
<head>
<title> MyWebsite <?php echo '~'.ucwords($desired_page); ?> </title>
<?php
$page_array = array(
"home", "blog", "news",
"links", "about", "contact"
);
$desired_page = (isset($_REQUEST['page'])) ?
$_REQUEST['page'] : "home";
if(!in_array($desired_page, $page_array))
$desired_page = "error";
?>
</head>
<body>
<div id="menu">
<ul>
<li <?php if($desired_page=="home")
echo("class=\"current\""); ?>>
Home</li>
<li <?php if($desired_page=="blog")
echo("class=\"current\""); ?>>
Blog</li>
<li <?php if($desired_page=="news")
echo("class=\"current\""); ?>>
News</li>
<li <?php if($desired_page=="links")
echo("class=\"current\""); ?>>
Links</li>
<li <?php if($desired_page=="about")
echo("class=\"current\""); ?>>
About Us</li>
<li <?php if($desired_page=="contact")
echo("class=\"current\""); ?>>
Contact Us</li>
</ul>
</div><!--end menu-->
<div id="content">
<?php
/*Includes specific page content*/
include("content/".$desired_page.".php");
?>
</div> <!-- end #content -->
</body>
</html>
I have a index.php, it calls a function post_themes_front(), the function gets all the "posts" from the SQL database and creates all the content, including the links. Example: picture A links to www.index.com/picture_a_name
code sample:
while($row = mysql_fetch_array($result))
{
echo '
<div class="post">
<h2>
'. $row['name'] .'
</h2>
<div class="infos b20">
By <em>MeetThemes</em> on <em>'. $row['date'] .'</em>
<span class="label label-important pull-right">'. $row['views'] .' views</span>
'. $row['comments'] .'
'. $row['catagory'] .'
'. $row['catagory'] .'
</div>
<p>
<a href="http://www.meetthemes.com/'. $row['name'] .'" title="ThemeForest - Silicon - Responsive WordPress Theme">
<img alt="ThemeForest - Silicon - Responsive WordPress Theme" src="img/'. $row['image_path'] .'" class="post_img" />
</a>
</div>
';
}
There is no such file as "picture_a_name", id like to create that, when the user clicks on it, and send him there with the apropriate values for comments, link etc, fetched from SQL.
But is it to late to create it on click?
Does it have to be created before that? i could make a function to create the content of all posts in index, and call it on index, and send them there on click but that would take up quite alot of client resources ..... in therms of loading time (I know its server side)...
Any suggestions are welcome
Example of what i want to achive, newone.org the frontpage, contains loads of "posts" which all link to seperate content.
I understand that there are plenty of CMSes that will do this for me, but i dislike using drupal / joomla / wordpress
What you can do is make an .htaccess file which catches all requests to non-existing files, something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ createContent.php?file=$1 [QSA,L]
</IfModule>
So that, when 'picture_a_name' doesn't exist, the request is redirected to createContent.php?file=picture_a_name. In createContent you can now create the asked content and save it to 'picture_a_name'.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I previously asked this question but need more help. Read this, then I will provide exact details at the end.
I have successfully transformed the static website I built for my podcast (www.noformatpodcast.com) into a dynamic website in Dreamweaver (on a testing server for now). I have done this without having to learn PHP or MySQL really at all by using the "Database", "Bindings" and "Server Behaviors" tabs in Dreamweaver. THE ONLY THING I AM NOT ABLE TO FIGURE OUT is how to get a page to generate dynamically when some one clicks a link using information about that link to create the page. For example, I have a grid of dynamically generated episodes on the index.php page. When someone clicks the link for, say, episode 55, I want it to load the episode page (which I have already created) and fill in the information from episode 55 such as date, title, blurb etc. If someone clicks the link for episode 43, I want THE SAME page to load and dynamically populate the the page with the info from episode 43. Is this possible to do with Dreamweaver's GUI, or do I need to grow up and learn to actually code?
The answers I got both told me I need to learn how to use $_GET. I've reviewed the basics of it at w3schools.com but still need a lot of help. Here is my dynamic repeating block of code that makes the grid of episodes on my index.php page
<div id="post_container">
<?php do { ?>
<div id="post">
<a href="episode.php" onmouseout="this.style.opacity=1;" onmouseover="this.style.opacity=0.7;">
<h2>
<span class="darkblock"><?php echo $row_Episodes['post_id']; ?> | <?php echo $row_Episodes['ep_title']; ?>
</span>
</h2>
<img src="ep_artwork/ep_<?php echo $row_Episodes['post_id']; ?>.jpg" width="260" height="260" />
</a>
</div>
<?php } while ($row_Episodes = mysql_fetch_assoc($Episodes)); ?>
</div>
So basically I have a bunch of big square images that are links to the episode.php page. I have coded the episode.php page to display data dynamically as well:
<div id="summary">
<div id="artwork">
<img src="ep_<?php echo $row_Episodes['post_id']; ?>.jpg" width="260" height="260" />
</div>
<div id="ep_title">Ep. <?php echo $row_Episodes['post_id']; ?> | <?php echo $row_Episodes['ep_title']; ?>
</div>
<p class="episode_date"><?php echo $row_Episodes['ep_date']; ?>
</p>
<p><?php echo $row_Episodes['ep_blurb']; ?>
</p>
</div>
<audio src="ep_media/ep_<?php echo $row_Episodes['post_id']; ?>.m4a" controls="controls">
</audio>
What I lack is the code to glue these two things together. I mean, as it stands now, if you click on any episode, it just takes you to the episode.php page and displays the info of the first episode (cause it's the first in the database). I need to make it show the info OF THE EPISODE CLICKED. And I guess that involves using $_GET. Could someone show me EXACTLY what code I need to include, and where to include it? Is what I've posted here enough? Or do you need to know more info about my MySQL database as well?
There are many solutions, but the simplest I think would be if in your first page (the grid page or index.php), you make your links as follows:
<div id="post_container">
<?php do { ?>
<div id="post">
<a href="episode.php?episodeID=<?php echo $row_Episodes['post_id'] ?>" onmouseout="this.style.opacity=1;" onmouseover="this.style.opacity=0.7;">
<h2>
<span class="darkblock"><?php echo $row_Episodes['post_id']; ?> | <?php echo $row_Episodes['ep_title']; ?>
</span>
</h2>
<img src="ep_artwork/ep_<?php echo $row_Episodes['post_id']; ?>.jpg" width="260" height="260" />
</a>
</div>
<?php } while ($row_Episodes = mysql_fetch_assoc($Episodes)); ?>
</div>
Then in your episode.php page, you should read the get variables using
$_GET['episodeID'];
A suggested code would be:
$postID = $_GET['episodeID'];
// WRITE CODE TO GET CORRESPONDING TITLE, DATE, BLURP, AND OTHER INFO FROM THE DATABASE FOR THE SUBMITTED POST ID
// ASSUME THIS TITLE IS SAVED TO $postTitle, $postDate, $postBlurp, ...etc.
<div id="summary">
<div id="artwork">
<img src="ep_<?php echo $postID; ?>.jpg" width="260" height="260" />
</div>
<div id="ep_title">Ep. <?php echo $$postID; ?> | <?php echo $postTitle; ?>
</div>
<p class="episode_date"><?php echo $postTitle; ?>
</p>
<p><?php echo $postBlurp; ?>
</p>
</div>
<audio src="ep_media/ep_<?php echo $postID; ?>.m4a" controls="controls">
</audio>
You have to make your code secure as well. But this is the idea behind it.
My advise is to learn php and mysql to make life easier.
Cheers.
To do this use apache rewrite module
This module will help you to rewrite urls which will then forward your requests to a specified script.
See below how to use .htaccess file to rewrite urls.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Now all you have to do is to parse the $_SERVER['REQUEST_URI'] and then interact with the Database to fetch the required content.
I'm currently using Wordpress and I have website listings that has link names as titles (eg. www.test.com, www.test2.com)
With this php code, it calls out the name of the website link:
<h3 class="list"><a class="h1" href="<?php the_permalink(); ?>"><?php the_title(); ?></h3>
Now that website has a "readmore" button and a "visit website" button. I'd like to turn the "visit website" button into a external link using PHP.
For example, the website listing is called "www.test.com". I'd like to turn the "visit website button" into an external link that will make it go to "www.test.com".
Here is my html code for the "visit website button":
<div id="visit">
Visit website
</div>
I hope someone can help.
Thanks!
I suppose you could do something like this instead
<h3 class="list"><a class="h1" href="http://<?php the_title(); ?>">Visit Website</h3>
If the_title is a valid URL starting with www (not http://), then it'll work. If they already have http:// in the title, remove that part.
Whenever you click a link you are visiting the url specified in href attribute.
For example to create a link, to go to google.com I would do this
Go to Google
So in you case you replace # with your link
<div id="visit">
Visit website
</div>
code it in php:All i can think of is this code:
<?php echo "<a href='http://www.test.com'>visit site</a>" ?>
I guess you are confused by
<?php the_permalink(); ?>
the_permalink(); calls get_permalink(); which finally provides the link.You can see its source code in wp-includes/link-template.php.