URL Generation Technique with PHP - php

I have a build a web portal based on the Cricket concept,
I have build a Custom based CMS where I can upload the News for the site
Once I upload the news, the URL Will be like this
http://cricandcric.com/news/news.php?id=841&An-emotional-moment:-Dhoni.html
But I am trying to have the above Url as follows (some thing like this)
http://cricandcric.com/news/An-emotional-moment:-Dhoni.html
Or similar to Stackoverflow.com,
Can any one please help me how can i build that?
Do I need to rewrite the URL ?

Yes, this is typically done with URL rewriting. This is a great intro to it.
Note that you'll have to make sure that the titles are unique, if you're removing the ID from the URL. (And you might as well get rid of the ".html" too)

Yes, as far as I can see you need to rewrite the URL. The URL "http://cricandcric.com/news/An-emotional-moment:-Dhoni.html" might be rewritten to "http://circandcric.com/news/index.php?title=An-emotional-moment:-Dhoni". Then, index.php has to figure out the correct news item from this title. This might be a source of problems (transformation of the title into URL-compatible string and back again), and, as Chad pointed out, the title has to be unique. If you get into trouble, check out how Stackoverflow handles this (ID as part of the URL, then title).

yeah Dude, .htaccess file will resolve all your Riddles.....Chk This--- How to use .htaccess file for rewriting URL?

Related

Remove index from URL after file extension has been removed using htaccess

With help on my last question about removing file extensions from the address bar if the page name and file extension were keyed in directly even after using htaccess to remove them, HERE I'm now wanting to remove index from the url. Specifically, if I type in my webpage's URL into a browser like so: http://webpage.com that exact address is what I see in the browser when the page is served up. However, if I click the Home link on my page, which contains a href="index" naturally, what shows in the address bar is http://website.com/index Certainly, I could change the link in my code to a href="http://website.com" to omit the index portion from showing up in the address bar, but that seems like a hack.
I found an SO article HERE that shows a method for removing index.php but I'm a bit lost as to how, or even if the code in that answer (I get particularly stuck when seeing Rewrite Base /
) can be blended in with the code that was given to me HERE to just remove index after the file extensions have already been removed.
My apologies in advance if this isn't a clear explanation, I'm in new territory here. Many thanks in advance!
Doing proper rewriting requires you to build the correct links that you want to see being used by the web (i.e. users and search engines). Don't use URLs in your own links that point to URLs that you consider to be wrong.
The link to the home page likely should be <a href="/">, if you don't like index.
Trying to fix the problem by making the client do two requests, one for index, then receiving a redirect to /, is the hack here. Fix your links - they are under your control for a reason.
Certainly, I could change the link in my code to a href="http://website.com" to omit the index portion from showing up in the address bar, but that seems like a hack.
No, that's what I would do :-)

htaccess or php redirect to this task?

I need help from someone with experience :)
I have noticed that lot of sites with SEO friendly urls like www.site.com/123456/some-title only take the value 123456 and doesnt matter the rest, htaccess just send the variable 123456 to the specific url and then the visitor see the content.
So, if I type
www.site.com/123456/some-title-1
www.site.com/123456/some-title-2
www.site.com/123456/some-title-3
www.site.com/123456/some-title-4
I will always see the same page because the id passed is always the same 123456.
But I saw a few sites correcting this. For example:
-If I type any of the 4 urls above, they show me the content BUT they redirect me to the right url (www.site.com/123456/some-title) first.
How can I do that? I think this is a php question, since they need to connect to the database to check the original "url path" for the id 123456, right?
If someone can give me a tip to do that would be great!
Thanks
How can I do that? I think this is a php question, since they need to connect to the database to check the original "url path" for the id 123456, right?
This is a PHP question. 2 things need to happen here. When someone requests:
www.site.com/123456/some-title-1
There is routing that happens in htaccess, something like (example rules):
RewriteRule ^([0-9]+)/(.*)$ /some.php?id=$1&slug=$2 [L]
So whatever the script is (some.php), it'll get two parameers, an ID and a slug. The ID is what it uses to do a lookup into a database or wherever the actual content is stored. When it fetches that information from the database, it can compare slugs. If they differ, then instead of serving the content, redirect the browser to show the correct slug. Then we start back over from the beginning, browser submits request (this time with the right slug for the ID), rewrite rule routes it to some.php and because the slug is correct, the actual content gets served to the browser.

Using PHP, HTACCESS & MySQL for URL

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

Change what a link looks like with .htaccess

I have a Joomla based community site and with search engine friendly URLs activated in the backend my profiles are located under mysite.com/community/profile/user/"username"
I need the htaccess file to do nothing unless a URL containing "community/profile/user" is found. If that string is found then it should change the link to mysite.com/"username" but in reality be showing the page mysite.com/community/profile/user/"username"
I think this would be rewrite rule instead of redirect, but I barely know what I'm talking about.
Can someone please tell me what code I must place in my .htaccess file in order to change this? I believe .htacces would be the best way to do what I need, but if you have another idea I'm glad to hear it.
First be sure you understand .htaccess's role.
It is only read when an incomming request is made. So it will not change URLs generated by joomla.
You can however allow urls like mysite.com/eddie to actually pull content from mysite.com/blah/blah/eddie
http://httpd.apache.org/docs/current/rewrite/remapping.html
If you are looking to "train" your users, you can add a step before that to redirect the URL as well. This get's very tricky though as if you're not careful you can get caught in a loop.
user clicks mysite.com/blah/blah/eddie
apache redirects to mysite.com/eddie
(browsers makes second request, user sees URL change)
apache sees mysite.com/eddie and loads the underlying mysite.com/blah/blah/eddie
An easier solution might be to tweak the joomla community code to generate the short urls (mysite/eddie) and use apache to make a call direct to the plugin (mysite/components/communit/index.php?user=eddie

Take Unique id and use as url for page

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.

Categories