Clean URL structure for php page without having a real page - php

I have a page where I am loading up a JSON file and matching data based on a users search.
The caveat however, is that I want to have really clean URLs for these results without actually making a new page for them. (For the life of me I don't know what the terminology for this is)
So when a user goes to website.com/names/adrian it will just land on /names/ and load the data based on "adrian".

You can do that with apache's rewrite rule:
RewriteEngine on
RewriteRule ^(names\/[a-z0-9A-Z-_]+)$ names.php?name=$1
Add this to your .htaccess file
It will send example.com/names/aName as get request to names.php.
And you can get that with $_GET["name"]; in names.php
By the way, you can see regex result in here: https://regexr.com/415mq

Related

cms without view template inside url

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.

htaccess change requested filename

I need to do this using htaccess
When a request is made for http://www.example.com/home, it should (internally)load the page http://www.example.com/home_st.php
Similarly when a request is made to other link called products (http://www.example.com/products), it should (internally)load http://www.example.com/products_st.php
In short what it is doing is appending "_st.php" and loading that URL. But one thing I do not want is if the user directly types http://www.example.com/home_st.php or http://www.example.com/products_st.php in the browser, it should show 404 / Page not found error
I have few other pages in that folder and I want those pages to behave in this manner. I understand the htaccess should have something like this
Turn on the rewrite
Forbid access if the URL is called with page names like home_st.php, products_st.php etc.
If it's "home" or "products", then rewrite(append?) it to home_st.php and products_st.php respectively. I have other files too while need to follow the same
P.N: My URL should not show the actual filename, for example home_st.php, products_st.php etc. It should only show as http://www.example.com/home, http://www.example.com/products etc
htaccess and regex is not something that I am well acquainted with. Any help would be great. Thanks
You want to be able to re-write URL's
This has been written before but i'll say it again.
You want to use the Htaccess file and the re-write rule.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^pet-care/?$ pet_care_info_01_02_2008.php [NC,L] # Handle requests for "pet-care"
This will make this url: http://www.pets.com/pet_care_info_07_07_2008.php
Look like this: http://www.pets.com/pet-care/
Links to more information: How to make Clean URLs
and for the webpage I used to reference this information from: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
To get your pages set the way you want them, I would advise that you read the two articals and try get what you are looking for.
If you need some specific help with doing this, ask.
Hope that helps.

Using mod_rewrite for user pages

So for now, when you use my search function to search a user and go to their page, you will get something like this. "http://www.mywebsite.com/profile/?pid=username"
The php on that page then gets the GET variable, and runs a query to get all of the information needed.
I would love to get rid of the ?pid= part and have just "http://www.mywebsite.com/profile/username"
But I am having trouble understanding any of the results I get with google on this specific URL. Most examples involve PHP files, which I'm using wordpress and embedding my php via execphp.
This should work for you in a .htaccess file:
RewriteEngine On
RewriteRule ^/profile/([^/]*)$ /profile/?pid=$1 [L]

How to make form that creates a new URL in a website?

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.

301 redirect with PHP and MySQL on 404

I am transferring a large static website of 500+ pages to a Joomla installation but what I am wanting to do, rather than code a 500+ line .htaccess file, is to use PHP's built in 301 header redirect to dynamically redirect requests that come in to the new URL.
So for example, after I move the site, the following URL would 404 without a redirect-
http://www.foo.com/old_page.html
I would want it to redirect to
http://www.foo.com/old-page.php
So I would like to create a MySQL database of old URLs and their corresponding new URL and then, if the server is going to return a 404, for it first to check the DB for the existing old URL, and if matched, to issue a 301 redirect to the new URL. Right, am I making sense here?
How would I do that? I can "talk" the logic but not really sure where to begin or how to accomplish it because, while I understand PHP, I don't really understand how this would work with Apache (or even if it would for example...)?
And if you know of any scripts out there that are already doing this, all the better.
In your Apache configuration, set an ErrorDocument of whatever /404handler.php script you like. In its run context, $_SERVER['REQUEST_URI'] will be the URI of the original page requested. Do your database lookup, and if you find a page you want to redirect do, use header("Location: " . $wherever) to redirect to it.
The simplest way that I can think of would be to use mod_rewrite (link is to a great introduction tutorial) to redirect ALL incoming requests to a single PHP script (or use a flag so that this redirect only happens if the request is for a file that does not exist), with the requested address passed as part of the query string. From there, have the PHP script look up where that request should go to, and redirect to it.
So if someone tries to open http://www.foo.com/old_page.html, the mod_rewrite would send to something like http://www.foo.com/redirect.php?page=old_page.html. redirect.php then does a database lookup to see what the new address for "old_page.html" is, and redirects to there.
Instead of creating a database, why not just put a redirect into a page with the old URL (ie. old_page.html)?
Since you are obviously doing this on a case-by-case basis with the database entries, you could just do it as a case-by-case basis with the pages.
You could write a quick perl script or whatever you like to generate these html pages for you given an old page's name and the URL of the new page.
You could also use a RewriteMap:
old_page new-page
# …
But you will need to define that map in you httpd.conf (or virutal host configuration):
RewriteMap oldtonew txt:/path/to/file/map.txt
You can then use that map in your .htaccess file:
RewriteCond ${oldtonew:$1} .+
RewriteRule ^(.*)\.html$ /%0.html [L,R=301]

Categories