search engine friendly blog urls - php

Hey i am in the process of making a blog for my site, i have decided to make my own rather than use a 3rd party, however i cannot figure out how to make the url's seo friendly atm they are like this /blogpost.php?id=15 however i want it to be /the-title is there any way to do this without htaccess? atm i am using this to make the urls'
echo '<a href="blogpost.php?id='.$row['id'].'">';
Then i am using _GET to get the id and then get the information from the database, i could replace id with name but that gives me /blogpost.php?title=The-Title
Thanks for the help.

In your htaccess you put something like this (if using apache)
RewriteEngine On
#ignores real files
RewriteCond %{REQUEST_FILENAME} !-f
#ignores real directorys
RewriteCond %{REQUEST_FILENAME} !-d
#ignores symlinks
RewriteCond %{REQUEST_FILENAME} !-l
#forwards the request onto index.php with the orignal querystring still intact
RewriteRule (.+) index.php?url=$1 [QSA]
Then any thing after the / will be forwared to the $_GET['url'] in index.php.
Then in index.php you can have a bootstrap class that will explode this by /.
Then you can load the appropriate class based on the first element in this array.
For example if the url was /articles
it would load the articles controller based on the /articles
Or if the url was /articles/about-something
The articles controller will load the article by fetching an article from the database with the about-something slug.

Related

php: Using subfolder path to access posts rather than GET

I'm sorry if there is already a question on this I just couldn't find it since I don't know what it is called.
Basically, I want to be able to access certain posts using their id without using GET. For example, I would like to access a post using website.com/post/crazy-clickbait-post instead of something like website.com/post.php?id=crazy-clickbait-post. It's just more user-friendly from an advertising perspective.
How would I go about this? I was thinking that maybe there would be a way to use a main post page in the post folder called something like index.php and then that would parse the url to get the post id but I'm not sure if that's possible.
You basically want 'SEO friendly' URLs like the following topic:
How to write htaccess rewrite rule for seo friendly url
In your case you need to add this (or create) to your .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^post/([^/]+)$ /post.php?id=$1 [L]

Imitate the page creation/url rewriting effect of this strawpoll.me

I am looking to do something along the lines of what is shown here:
http://strawpoll.me
When you create a poll on there it gives you a unique url to which you and anyone else can access it by visiting that url.
Now obviously they can't be cluttering up their server with thousands of individual files containing the polls and I'm assuming they have one basic template to which they load the poll information from a database.
I want to make a form and when the user submits it they get their own unique link like how it is on the strawpoll website.
As is it is a simple dynamic page creation requiring only few files...
The poll creation page which is static
The processing.php file which collects the data and inserts into the db
The dynamic poll rendering page polls.php which renders the poll based on an auto-incremented id assigned to the poll.
The polls-results.php which returns the results.
http://thepollsite.com/12345
.htaccess is used to identify which poll to render as well as which results to render.
# Turns on Mod-Rewrite Engine
# ----------------------------------------------------------------
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
# Poll Pages
# ----------------------------------------------------------------
RewriteRule ^([^/]+)/?$ polls.php?id=$1 [QSA,L]
RewriteRule ^([^/]+)/?$ polls-results.php?id=$1 [QSA,L]
On both polls.php and polls-results.php you get the id with: $poll_id = $_REQUEST['id']; and process from there.
The embed code is rendered in a simple dialog pop which provides the iframe code as shown.
Though there are frills you can add... its is really a simple process.
On the .htaccess side, you'll want something like:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
With that in place, all requests for files/directories that don't already exist on the server will run index.php instead of showing a 404 page. Then, to get the requested path, use $_SERVER['PATH_INFO'] in your index.php.
This is the general method of using "clean" URLs with PHP. It's probably already been said in another SO answer, but it's hard to find so I wrote it again.
Related:
Why use a single index.php page for entire site? and How to do URL re-writing in PHP?

Create a view from slug blog title in plain php

I've seen a lot of material on how to generate a unique slug title and then store it in the database.
I want to make a blog with urls like blog/my-first-post and then get the content from the db and make the view.
Let's say I will have a very basic table with columns (id, title, body, slug) and I already have the generated slug in the database for each post.
How can I make a simple router in PHP that gets the url blog/my-first-post get the slug and search for it in the database and then get the contents of that post from the db so I assign the blog contents into an array and then make the view for that url?
All this without using a PHP framework if possible.
You could route everything to an index.php page with htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Using this htaccess file, the URLs would look like www.website.com/blog/slug-name
The slug-name part of the URL can be accessed with $_GET['url']
Then just get the relevant data from your database that matches the slug:
SELECT title, body FROM blogs WHERE slug = $_GET['url']
Obviously don't use that SQL, it's just an example. Make sure you use prepared statements

htaccess need help rewriting url

I've been trying to rewrite my URL's with a htaccess file.
My project is using my index.php as it's basic controller.
I fetch the pages from the database with the get-value "naam".
Example:
localhost/YourDesigns/YDCMS/index.php?naam=home (this shows the homepage)
localhost/YourDesigns/YDCMS/index.php?naam=about (this shows the about page)
Now i want to rewrite the URLS to be shown like this:
localhost/YourDesigns/YDCMS/home (this shows the homepage)
localhost/YourDesigns/YDCMS/about (this shows the about page)
I have done some htaccess stuff myself and i've successfully removed the "index.php" from the url, but the "naam" still remains.
How do i remove this?
This is my current htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /YourDesigns/YDCMS/index.php?naam=/$1 [L]
Thanks in advance :)
I think your way to see the process it's not totally right, if you want to show the url like you say localhost/YourDesigns/YDCMS/home you have to first replace the url inside your html content to that format, then by using a RewriteRule you call the correct path internally:
RewriteRule ^desired_url_path/([a-z0-9\-]+)$ /base_url_path/index.php?naam=$1 [L]
this way when an user click on a link the Apache server will use the above regex to convert the url by a rule that basically says : everything after the base url is aparameter value to be passed on the index.php.
Naturally the regex can be modified to suit your needs, the one i've written above it's a basic string pattern.

SEF url without name with core php and .htaccess

I want to make SEF url In that i want to remove file name also from the url.
my site is in the core php, I manage to remove the extension from the url but didn't have idea that how to remove the whole file name from the url.
As i searched on google i got this code for the .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I have no idea that how to write header in php so it goes in related file but don't show the file name in url.
<a href='http://mysite.com/edit/user/2/1'>click</a>
Now if i click on above link it does goto edit.php with url "mysite.com/edit/user/2/1",
so from this how can i remove file name that is "edit".
Thanks in advance
Why? Isn't that a SEO friendly and user friendly URL? Why would you want to make it un-user fiendly? But anyway my guess is that you want /user to point to /edit by the looks of it:
RewriteRule ^/?user/*$ /edit.php?type=user&%{QUERY_STRING}
Though you will be making your site worse by using this and adding uneeded complexity to not only site maintenance but also user navigation.

Categories