How to define friendly URL rules using .htaccess? - php

Long time I am trying to define Rewriting rules but I could not succeed yet? I have a simple page http://www.myURL.com/pdf.php?id=2. I want to make it friendly by this: http://www.myURL.com/pdf/2/ ...
I wrote this rule after spending time on google:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^pdf/([0-9]+)\$ pdf.php?id=$1.
When I uploaded my .htaccess file on server and try to run first it give me 500 ERROR. Second time it loaded page but it could not show me friendly URL; it was showing as before.
Kindly tell me better solution, I followed many instructions to make it useful but could not successful. My Client are not going further unless I showed him friendly URL. kindly help me as soon as possible. This is my client hosting Organization: Justhost.com.

You can do this using Apache's Rewrite Engine. Something like this:
RewriteEngine On
RewriteRule ^pdf/([0-9/]+)$ /pdf.php?id=$1 [L]
This will allow you to go to http://yoursite.com/pdf/192 instead of http://yoursite.com/pdf.php?id=192

Here is a very simple tutorial on using Apache's mod_rewrite to create friendly URLs.

Probably a bit too much to take just right now but you could look at some of the available MVC frameworks to help you solve this. I'm guessing that the host only offers PHP 5.2 so I'd take a look at the routing/controller solution from Zend Framework 1.12 to solve this as that can be implemented into existing code without too much trouble.

Related

Rewrite Rule Issues

I hate to write this post but i've been working on this for far too long.
We are upgrading a multilingual site and the language module that we are now using handles URL's very differently. We are moving from domain.com/location-fr-fr/ to domain.com/fr_FR/location/. I have been able successfully handle rewriting that by using the following RewriteRule (.*)-fr-fr fr_FR/$1/. I cannot figure out the english one which looks like this. domain.com/location/ has to go to domain.com/en_US/location/
Where im running into the issue is I can't just rewrite everything, as I need to exclude any url beginning with /en_US or /fr_FR, but im failing with that.
I have tried this which i really think should work
RewriteRule ^(?!en_US/) http://www.google.com
I tried adding in a preceding slash but no good either
RewriteRule ^(?!/en_US/) http://www.google.com
https://htaccess.madewithlove.be/ agrees with me that it should work too but in practice it does not. I have been doing all my testing using wget so that browser caching is not an issue.
Can someone give me a couple of pointers? I'm open to any suggestions on how to make this work better.
It's a Silverstripe website in case that affects rewrites too.
Edit:
After removing Silverstripe rewrite rules the rule works fine.

how to implement url rewriting in php

I am developing a php web site. Here I want to make the site as clean URL.
My index page is domain/news/index.php?i=1. I want to display this URL as clean URL. But I am the beginner of URL rewriting. Does anyone help me??? How to write this URL
Thanks in advance
You need to add an .htaccess file into your root and define the rewriting things there. This is not "in PHP" because PHP doesn't provide a url rewriting method. It's the server (Apache) that read url rewriting files such as .htaccess.
References
Url Rewriting Guide
You can take either of two approaches. The first is to make correct rewrite URL for all links in .htaccess, and the second is to redirect all links with .htaccess to index and "possess on it" with PHP.
Rewriting is easy and there are many relevant docs to be found on the web. Please search first.

URL re-writing in PHP

I am trying to implement URL rewriting in my PHP application. Can someone share a step by step procedure of implementing URL rewriting in PHP
In my application I want to implement following URL rewriting
http://example.com/fast-five
http://example.com/300
http://example.com/13-b
from
http://example.com/movie-download.php?nm=fast-five
http://example.com/movie-download.php?nm=300
http://example.com/movie-download.php?nm=13-b
One more thing which URL will be best according to SEO, management, application point-of-view out of the following two types.
Of course
http://example.com/fast-five
will be good for SEO
Are you serving your PHP through an Apache HTTP Server installation? If so:
RewriteRule ^/fast-five$ /movie-download.php?nm=fast-five [R=301]
From an SEO perspective, the first would be preferred. Using the HTTP 301 ("Moved Permanently") is most effective for this.
If your using an MVC framework like CakePHP, you should look at the documentation on routing. Otherwise, you can use the web servers rewriting rules.

Website structure / Newbie

I created a website using php, passing values from page to page either with POST or GET.
Though there is some cons, I dont know how to track specifically what data has been viewed in GoogleAnalytics as it just shows the name of the page (xxxx.php)
On the other side, I see websites structured differently with a bunch of subdirectories created : www.xxx.com/xxxxxx/xxxxx/xxx
This looks like pretty manual for me , compared to the .php?xxxx=xxxx way of structuring.
Do you know how this subdirectory style structuring can be automatically obtained?
This is done with Apache rewrite rules.
To make it so that whenever a user goes to /posts/visiting_new_york, it actually goes to to /viewpost.php?id=visiting_new_york, you create a file in your site called .htaccess like this:
RewriteEngine On
RewriteRule '^posts/([^/]+)$' viewpost.php?id=$1 [L]
Use an MVC framework like rails, or simply configure your webserver's virtual directory structure to be identical to the local servers file system and adhere to that scheme when saving your php files.
Yes, you can do this with "mod_rewrite" in apache.
It involves creating a .htaccess file with URL re-writing rules inside.
So you can transform /index.php?page=contact&lang=en into /en/contact/
Here's a good rewrite cheat sheet: http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/
Wadih
You need to read about url rewriting
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
If you just want to track your dynamic pages , there is another solution in Google analytic
http://www.google.com/support/googleanalytics/bin/answer.py?answer=55504

Need to implement or duplicate url rewritings for membership project - but don't want to use heavy framework

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.

Categories