Using PHP, HTACCESS & MySQL for URL - php

My site has pages like news.php, vidoes.php, products.php etc
The data for these pages are held in a MySQL database they have id, name, description and permalink
If a name is News Article Title or Boxing Match Video they permalinks would be news-article-title and boxing-match-video
Now on the news.php page there is a list of news articles and they are linked to read-news.php with ?id= and the article id beside it.
So a link looks like this www.mysite.com/read-news.php?id=2
On my read-news page I use a GET to get the id and then data for that article comes too.
How do change the links on the news.php page to come up as www.mysite.com/read-news/news-article-title.php and the URL to display the same when reading it.
I cant seem to find a tutorial that shows exactly what I need.
Any help please
Thank you

You have to use a .htaccess file (if you are on a Apache server and do not have access to the server itself, like in a shared hosting situation)
.htaccess is a simple textfile you make and place in the directory in which it controls the access, e.g. the redirection of URL's and has a lot of usable functionality.
With this you can have your URL's rewrited without the user noticing it.
Good for SEO purposes.
More info: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you want a quick try, automatic generation is done at
http://www.webconfs.com/url-rewriting-tool.php

Related

How to redirect old URL from a custom PHP website to new WordPress URL?

I have a client that needs to rebuild their site from custom PHP website to WordPress instalation. The client run a local news site and has tons of news articles in it. Approximately more than 3000 URLs.
The old URL structure look like this:
http://localnewssite.com/news-85720-trump-is-the-king
I need to move them to the new URL like this:
https://localnewssite.com/category-name/article-title
I had two problems in here.
The first one is the old article URL has no category for each of the article, so they just use single "news" category for all of their article URLs and the article's ID number in the database despite their site has 12 news category in it.
The second is I counted approximately more than 3000 articles with that old URL style that I need to move.
I was only given the MySQL database file as the old developer refused to give any access to my clients for their own server.
My client intended to move the site to a new VPS account, so this could be tricky.
Is there any way to use regex or any HTACCESS to automatically and easily redirect 301 all of the old URLs to the new one?
I can't manually move each of the article and reconfigured all of the URL for SEO using Yoast plugin because it would take a lot of time.
Thank you.
You can either use this plugin Redirection, Or write code in htaccess to 301 redirect old urls to new urls.
Redirect 301 /old-url https://yourdomain.com/new-url

Is it possible to implement virtual path in PHP-

I am using PHP script to set up a website which sells book.
Let say my website is hosted at www.sell-book-example.com.
I want to allow accessing to a book detail page by navigating to www.sell-book-example.com/book/2121.html where 2121 is the book id.
Keep in mind that book/2121.html should not exist in the document root of the web server.
As in I do not want to pre-create many book detail pages in a book folder.
I have checked some other website which are able to do that with PHP.
If anyone can provide me some sample or link, that would be great.
Thank you.
This approach is known as Dynamically generating of pages by php scripting.
You have to create a script.php file where your dynamic code will be scripted and from the url you have to catch the code and according to the code you have to generate the book detail page. All this should be done by scripting.
The url will be like these
www.sell-book-example.com/script.php?id=2121
www.sell-book-example.com/script.php?id=2122
www.sell-book-example.com/script.php?id=2123
This urls will not show in website. To make it pretty as you need like
www.sell-book-example.com/book/2121.html
You have to redirect in .htaccess file i.e. to map the urls.
For example if some one is trying to call www.sell-book- example.com/book/2121.html url than internally it will be call www.sell-book-example.com/script.php?id=2121 url and produces your book details page of 2121.

Creating an SEO friendly url with .htaccess and php from a mysql field

My website creates urls like the following: /index.php?placeid=27
I want to be able to change the url to display the place name, so something like:
/southampton
where the placename is another database field displayed on the content of each page. I guess this requires some php and .htaccess solution which puts a variable into the url, but I can't seem to work this out from any similar questions on here. Thanks.

PHP - Replace page ID selected from MySQL database with alias name in URL

Not sure if title explains my problem well...
I have a site with a lot of pages (I call them "sections"). When users click on a page link, PHP script selects section content and infos from MySQL database, and show them to user. To avoid the display of section ID in browser addresses bar (the page is loaded through the knowledge of section ID in database), there is one different file associated to each section. In these files, PHP code passes ID to $_GET variable, and script loads section. The displayed URL is so http://site_domain/path/to/file_name.php.
Is there a way to avoid the creation of real file and to show, for example, an alias in URL? I thought to add a new column "alias" in database table sections to replace it in URL.
Hope my English and the explanation is clear...
I think you can create some sort of general controller which is a page, lets call it index.php and in that page you can pass some information about the section on your website through some GET parameter.
For example: http://andreswebsite.com/index.php?section=home
And then in your index.php code you can redirect the user to that specific section. You can have the section name in your database so no need to pass the ID.
Also you can look at the Apache mod_rewrite module which I think it could help you (I don't know if you use Apache as your web server)
Here is the documentation: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
I hope this has helped you.

Friendly url when clicking news headline

For last 4 days I´ve been trying to make a friendly url using .htaccess (mod_rewrite)
I have a some news on frontpage and link redirects to the full article on news_id.php
So mypage.com/local/news_id.php?newsid=37 should take the headline from the "headline" field
in phpmyadmin - for example mypage.com/local/police-stops-girl-fight-at-the-mall
my .htaccess code is
RewriteEngine on
RewriteRule ^local/([^/.]+)/?$/local/news_id.php?headline=$1 [L]
Some tutorials says I also have to edit the php link on frontpage which is
<?php echo $row ['headline']?
...but I've also stumbled on articles that says that I don´t need it and show this should do
it.
I've checked out if mod_rewrite is enabled at the server and it works fine.
Am I close or is this much more complicated than I think?
Previously you were fetching by id. Now you are attempting to fetch by title.
That's a fundamental difference in the way your article lookups will need to occur and what content will be in your urls.
It may be easier to start out with a simple experiment site. Get that working. Then convert your real site over once you've seen how it all works.

Categories