I am building a website for sharing links and pictures for php experience. Currently, each post has a page where users can comment on it. Every post also has an id that is stored in a database with it. What would be the easiest way to make this id the url of the post? For example:
For the question that has an id of 4a3cd5, I would want the url for that question to be post.com/posts.php/4a3cd5. thanks for your answers.
In the HTAccess page (.htaccess), write the following code:-
Options +FollowSymLinks
RewriteBase /
RewriteRule ^posts/(.*)/$ /posts.php?id=$1
So now, in the Address Space, if you write http://localhost/posts/4a3cd5, the user will be shown a page corresponding to the post ID of 4a3cd5. Internally, the URL which will get processed is this one http://localhost/posts.php?id=4a3cd5. This whole technique is being done by HTAccess, and this way of showing URLs to users is called SEF URLs.
More on the HTAccess tips & tricks can be found here.
Now in the page "posts.php", you can write all the logic which you want using the PHP GET Superglobal Array "$_GET['id']".
NP: A special note - please try to avoid this type of coding. Instead try using any of the available standard PHP MVC Frameworks.
That would involve a lot, maybe you should just have URL's like /posts.php/?post=4a3cd5.
Overall this would be much more practical. You can just GET the post variable and connect to your SQL database.
Related
I'm currently looking for a way to change / hide the default WordPress admin-ajax.php URL. I want to do this to remove the admin from it to prevent misunderstandings by my customers when I use the AJAX URL to read for example a file from my server. In this case, the URL has the admin prefix which is a bit confusing.
So in result the AJAX URl should looks like this: ajax.php
I've searched a bit but I can't find any helpful information. Because of this I can't show you any related code. But when I got it, I'll update my question to help other people.
Alternatively and perhaps more effectively you could leverage WP API and create a custom endpoint to process your data so that there isn't url confusion.
I'm creating a simple, wordpresslike cms and keep all parts of a page in a table and load them using a template page named view.php following by a $_GET variable, for example:
sky.com/view.php?article=how-to-do-something
But as I can see on wordpress sites there is no 'view.php' template file and $_GET variables inside url. There is pure domain name and title of an article.
I suppose this is a better approach for SEO engines.
What is the general way to do this and how can I use the same. Is there a function to create a file on fly, or maybe a hidden file system...
The same is with stackoverflow.com. There is no view template inside url, but I'm rather sure it uses database table for storing parts of a page.
I tried with .htaccess file and this accepted solution but got the error 500 - internal server error.
By way of explanation; what stackoverflow.com probably does is something like this:
URL:
https://stackoverflow.com/questions/44654672/cms-without-view-template-inside-url
Actual URL is first rewritten using mod_rewrite so that 1) the missing .php is added, and 2) the GET parameters are set:
RewriteEngine on
RewriteBase /
RewriteRule ^questions/([0-9]+) questions.php?qid=$1 [NC,L]
And so the questions.php page loads the question with id 44654672.
The above mod_rewrite says: take the URL, starting with questions/ and send that URL to questions.php, as well as saying: After questions/ take the number value and use that as the question id GET variable so that the PHP page loads the correct question from the database.
The wording in the URL is purely for SEO purposes.
You will notice that https://stackoverflow.com/questions/44654672 will also load your question correctly (*), but if you change the number value (even while keeping the words the same, ( such as https://stackoverflow.com/questions/44351172/cms-without-view-template-inside-url ) another question will be loaded from the database.
Putting the exact URL link into my answer makes it display the question title rather than URL
Think of it as the mod_rewrite is doing a find and replace search on the URL string. That's all.
I am trying to rewrite the following Url for SEO and userfriendly purposes using htaccess or other solution. I have searched and found many answers but none gives me multiple parameters and i am not advance at this.
mysite.com/myfile.php?dept=1&prodid=161&user=2018&ulevel=1&parentcust=1&prodname=My widget
to
mysite.com/products/mywidget
if My Widget has spaces or apostrophes i need them removed. example the product Tiger's eye widget should be tigerseyewidget or tigers-eye-widget
You don't have to solve your problem with rewrites only.
For starters, let's strip your query string:
user id & user level do not belong there - it's dangerous to have these in your query string. What if I change it in my browser to user=1&ulevel=5 - am I then suddenly the administrator with access to all area's? Instead, work with sessions and store the user id & ulevel there
Do you really need the product id as well as the product name in the query string? I'd say: drop the product name, the id should be enough information for you to retrieve the product name.
parentcust: what is that? Do you really need it in the query string? What if I change the value? Looks like something that belongs in a session as well.
After stripping that, all you're left with is myfile.php?dept=1&prodid=161 - now that's something that's much easier to rewrite!
How to rewrite is another question. There are plenty examples on SO how to rewrite a query string based request to a "pretty url". Changing Tiger's eye widget to tigers-eye-widget is also well documented.
But.. who generates these links? If you generate these links, why not turn it around and link your products to e.g. mysite.com/tigers-eye-widget.html and then in PHP map "tigers-eye-widget.html" to product id 161 & dept 1?
Your problem should really only apply to legacy URL's that people may have bookmarked or still appear in search results. But for those cases it's perfectly acceptable to redirect them instead of rewriting them. So you can keep the mapping in a database or textfile, process the request with PHP and do a 301 redirect to the new URL - that way you tell search engines to replace the old URL with the new one and you don't have to worry about complicated multiple .htaccess rewrite rules.
I have been thinking for quite some time now about how I could manage thousands of webpages that change based on a set of data in MySQL.
So in essence, I have thousands of "products" and each is represented by a row in a database table.
What I do currently to page each of these "products" viewable via a url is place a .php file in the url location so that when a user visits http://www.mydomain.com/some_random_product.php they are opening one of many .php files that all contain the following:
<?php
$thisPageName = basename(__FILE__, ".php");
include '/out/of/root/allProducts.controller.php';
?>
So essentially all is hand off the url name to another script that takes the url name then searches the database for a corresponding "product", if it finds the product then it displays a product page with the relevant information and if not it displays an error page stating that the product could not be found.
What I would like to do now, is better that process so that I would not need to create thousands of physical files on the server. Even though this is quite a simple task as I have a script to do it, it just becomes a pain having to run the thing all the time (I have to do it locally as it is a resource hog, then copy all the pages across to the server).
Possibly just one file that all of the urls in that location would load without changing the url as I need it to find the relating info.
I would like to add that the server running the domain is a VPS that I have full access to and I understand that the Apache mod_rewrite module may be of use here but I honestly would not know where to even start in terms of the logic behind implementing that.
If anyone could suggest a better way to do this or anything that may be of relevance then it would be greatly appreciated, thank you!
Pass the product ID in the URL and catch it in your PHP script with $_GET[productId].
Then you should query the database to get the row of the product matching that ID and display the data of that product.
To make the URL's pretty for this case and stay SEO friendly:
#.htaccess
RewriteEngine On
RewriteRule ^product/([0-9]+)/(.*)$ /product/product.php?productId=$1&name=$2
# URL:
<a href="/product/product.php?productId=123&name=product-one">
Product one
</a>
<!-- OR (Because of the URL rewriting) -->
<a href="/product/123/product-one">
Product one
</a>
# PHP Script
$id = $_GET[productId];
$sql = "SELECT *
FROM `product`
WHERE `id` = ?;";
etc...
This way you could show thousands of different content on just one simple PHP page.
It looks like you have several questions in one here.
Firstly you need to build a dynamic page that retrieves the product information from the database and displays this, based on a unique identifier passed to the page through a querystring/GET parameter.
Example of GET parameter usage in PHP from php.net:
<?php
echo 'Hello ' . $_GET["name"] . '!';
?>
Assuming the user entered http://example.com/?name=Hannes
The above example will output something similar to:
Hello Hannes!
Secondly you want to use mod_rewrite to allow pretty URLs to be accessed, i.e:
example.com/products/some_random_product vs example.com/products.php?id=some_random_product.
Example Apache mod_rewrite condition to achieve this:
RewriteEngine on
RewriteRule ^products/([^/.]+)/?$ products.php?id=$1 [L]
Based on the questions you've asked I would recommend you do some research into mod_rewrite, $_GET, and in general the use of PHP to create dynamic web pages.
Edit on SEO:
Providing you use mod_rewrite there should be no difference in SEO. The client won't be able to tell that you are really displaying products.php rather than /products/some_random_product. mod_rewrite is entirely server-side, it is completely different to sending a response to the client telling them the page has moved for example.
Example, on Craigslist users complete a form of what theyre looking for and once submitted it automatically creates a new URL hosted on the website URL. Sorry i don't know how to do this or what it is called. Anyone? How can i do this and what is it called? =) Thanks!
You mean like how StackOverflow has a URL for this question?
http://stackoverflow.com/questions/12662640/how-to-make-form-that-creates-a-new-url-in-a-website
All it takes is a simple .htaccess file, something like:
RewriteEngine on
RewriteRule /questions/([0-9]+).*$ /question.php?id=$1
The whole "how-to-make..." thing is just the name of the question. I'm not entirely sure why many sites put that in the URL, possibly for SEO purposes, but it will automatically be filled in if you leave it out so I'm assuming it's optional.
This is done by a (mysql-)database, php and .htaccess resulting in dynamic url's.
A dynamic URL is a page address that results from the search of a database-driven web site or the URL of a web site that runs a script. In contrast to static URLs, in which the contents of the web page stay the same unless the changes are hard-coded into the HTML, dynamic URLs are generated from specific queries to a site's database. The dynamic page is basically only a template in which to display the results of the database query. Instead of changing information in the HTML code, the data is changed in the database.