I'm trying to implement java like url pattern matching in php. Suppose i have a link which leads to a file called find.php (Example is shown below)
http://www.xyz.com/MyWebsite/TryToFind/find.php?search=abc+def
Then i want to expose the follwing link to the users, say,
http://www.xyz.com/Search?search=abc+def
then i want to match this(2nd url pattern) url to the one i have provided above(1st url pattern) using a controller. This is because i want to hide the exact uri pattern to the user and also the hide the extension which i have used.
Please Help Me Guys...
Regards,
Abilash
If you are using apache (which I assume you are as you have tagged .htaccess) as your http server then you can use Apache's Mod_Rewrite Module. It usually ships with most installations of Apache these days and anyways you can install one yourself if it is not present.
Mod_Rewrite will enable you to achieve what you desire, In fact it is designed for that purpose only. While php frameworks like CodeIgniter provide some programming support for Mod_Rewrite, working directly with .htaccess using the Regex will render you with more power and flexibility.
A full explaination is difficult here. But this was the tutorial I found handy when learning the same.
Related
I am considering building my PHP project in Google App Engine (GAE), and I would like to be confident that I could easily port my code to a more standard Apache Tomcat & PHP server if needed. The one aspect that I cannot find a standard implementation for is GAE's app.yaml handlers. Let me define the functionality that I care about:
The ability to explicitly route incoming requests to a specific php script for fulfillment based on URL patterns.
My searches thus far have lead me to the Tomcat .htaccess RewriteEngine. However, it seems like this literally re-writes the URL and redirects the client machine to that URL. Am I wrong? Other than this solution, I have found nothing else that is promising. Can someone suggest a replacement for GAE's app.yaml handlers?
Thanks in advance!
Seems like you are looking for Apache mod_rewrite which lets you setup rewrite rules that are interpreted behinds the scenes and do not change the external URL.
In wordpress there are directories which actually do not exists.
like example.com/post/name-of-the-post actually opens a main php file which fetches the content of that posts from database.
How can i implement this natively on linux apache php server without wordpress?
You need to use .htaccess. This is the technology behind Wordpress' ability to do what you are wanting to do.
Here are the docs from apache.
Also here's a nice Tutorial
I don't know why people use mod_rewrite, it is overkill for most tasks. For most things there are more nimble modules available. That is not to deny mod_rewrite, it has its place, but I find it is over used. In this particular case you can use mod_alias, no need for mod_rewrite. E.g.
AliasMatch ^/post/(.*) /real/path/post.php$1
module docs here: https://httpd.apache.org/docs/2.2/mod/mod_alias.html
This doc beautifully outlines when NOT to use mod_rewrite: http://httpd.apache.org/docs/current/rewrite/avoid.html
I'm new to PHP and web development in general, but have been a programmer for 5 years. I'm trying to work on my own website from scratch using Notepad to edit my PHP files and WAMP for my server. I'm able to view my files fine in Safari, Chrome and Firefox, but not IE (which we all know IE isn't the greatest) because I'm using some HTML5 stuff. Anyways, I have an Includes folder that holds my files for my header, menu and footer. I have an index.php file that includes these files and displays them fine. In the center of the page is where I want the content. To try and keep clean urls, I made quite a few folders and put this same index.php file in there (e.g. Profile/index.php, Forums/index.php, etc.). I did this so when I went to localhost/mysite/profile/ it showed me the template I wanted to use. However, there has got to be a better way to use the template and a better way to have clean urls. I'm not currently hosting this site anywhere so I don't know if I'll have access to the htaccess file (not even sure what it is honestly, just seen it mentioned), but I was curious of having the folder structure (one folder for each menu item) is a normal or ok practice? I was also curious if there is a way to use the index.php without having to copy and paste it every time I make a small change. Any help would be appreciated. Thanks.
If you are planning on templating I suggest using an existing platform like Symfony, Zend Framework, or Smarty. Smarty is probably the easiest to get going with and it is (almost) purely for templating. Whereas Symfony and Zend Framework are big packages with lots of bells and whistles.
If I was going to be doing pure templating I would look at Smarty. I use Zend Framework for just about all my current PHP projects now but it has a pretty steep learning curve. Your first couple weeks will be frustrating.
As far as URLs go, .htaccess is probably the preferred method (at least in my book). Zend Framework and Symfony both have kind of default URL writing style that looks like http://host/controller/action where controller would be Profile or Forums. You wouldn't necessarily have to see host/profile/index, it could be host/profile or host/profile/edit, where edit is the action being performed.
Copying the index.php file is not really the way I would go. It seems messy and there are a few other options. If you want the urls to be clean and search engine friendly you should really have a look at url rewriting using .htaccess
You're saying that you're not sure if you will have a server with "access to the htaccess file" but if you can upload files you can always upload a .htaccess file as well -- the problem is that the web server is not always using them or might not have mod_rewrite enabled. In this case you can get your urls on the format of http://www.example.com/index.php?u=/Profile/foo (this is also my preferred way to handle url rewrites).
In your index.php just make sure to read the requested url parameter passed by mod_rewrite and include whatever files in the folder that you need. Actually, you don't even need a "physical" folder structure, you might want to implement it using classes or something like that. (I'm sure I would).
I would really recommend that you go have a look at a good PHP framework, CodeIgniter for example. Even if you decide to code everything from scratch, you would still learn a lot about best practices for url handling, databases, the MVC pattern, template engines, database abstraction layers and PHP coding in general.
your answer is the htaccess file, is converts the 'folder structure' to $_GET value's
for example,
if you're in website.com/Profile/ you can write an htaccess line that will convert that into website.com/index.php?folder=Profile
htaccess:
RewriteEngine on
RewriteRule ^/(.*)/$ index.php?folder=$1 [NC,L,QSA]
RewriteRule ^/(.*).html$ index.php?folder=$1 [NC,L,QSA]
I am studying php routing material now, but I still can't understand the theory. Does anyone here have good tutorials or links to share?
You are talking about url rewriting, and there are many other questions about that on here if you search for that term.
Regardless, url rewriting isn't handled by PHP, it's done by the web server. If you are running PHP under Apache, then you'll want to have a look at mod_rewrite. For IIS, you would look into the IIS Rewrite module (which conveniently can import mod_rewrite rules from apache as well)
Take a look at this: Url Routing with PHP
I have a simple membership project and teh client wishes that urls be simple rather than having something like:
index.php?p=view-member&id=568157&hash=00218546598797898798162454712
he would like to see it as so:
website.com/member/username
Now I've worked with Zend and understand you need to do some htaccess alterations to get this done and this is like already easy to do in Zend Framework but I don't want to use the Zend framework for this as I already have a fully functional membership project I built which I would like to tweak for this project.
I thought of creating empty member folders with a simple php file that would include my main php file but am not sure of what complications I would run into if I use such a system. I'm open to ideas here guys. What do I do?
You don't need a framework to do this. Just use Apache URL rewriting with mod_rewrite. For example, in .htaccess in your document root (top-level directory that is served):
RewriteEngine On
RewriteBase /
RewriteRule ^member/(\w+)$ /index.php?p=view-member&id=$1
If you need some sort of hash to identify the user, don't put it in the URL (like your hash parameter is). Use a cookie.
You can of course use other Webservers such as IIS or nginx.
If you are running Apache a RewriteRule with mod_rewrite could do the trick. You also have to change index.php so that it takes username as a parameter and stores the hash in a cookie/session instead of in the URL.