I am building a php site(raw coding). I want to modify the url. i think htaccess is the solution. but i am weak is this. can u people help me about this? my urls are like these:
www.site.com/index.php
www.site.com/invoice_details.php?invoice_id=100
www.site.com/user.php?uid=20&type=1
www.site.com/client_details.php?cid=10&status=active
I want these to be like:(1st choice)
www.site.com
www.site.com/invoice-details/100
www.site.com/user/20/1
www.site.com/client-details/10/active
Or may be just add "slash" and replace the "_" with "-"(2nd choice)
You can solve that using mod_rewrite, which can be configured in .htaccess files.
For example, to solve your number 1 and 2:
RewriteEngine on
RewriteRule ^invoice-details/([^/\d]+)?$ invoice_details.php?invoice_id=$1 [L]
RewriteRule ^user/([^/\d]+)/([^/\d]+)$ user.php?uid=$1&type=$2 [L]
There's masses of information online, this http://www.workingwith.me.uk/articles/scripting/mod_rewrite seems like a reasonable introduction.
You're problem cannot be solved with just .htaccess though that is needed.
What want to do is called URL Routing and requires some code to handle the request properly.
Here's a decent tutorial: http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/
Related
I have got different pages in my website(portal) which may take one or more parameters from the URL.
So the URL www.example.com/xyz has to be solved into or like www.example.com/profile.php?id=xyz and the value id should be able to get using GET method.
I'm working on PHP. Any answers would be highly appreciated. (I'm new to PHP and have only basic+ knowledge in it. Please explain briefly.)
To go from: www.example.com/profile.php?id=xyz into www.example.com/xyz you can use the following rule in your .htaccess file:
RewriteEngine On
RewriteRule ^([^/]*)$ /profile.php?id=$1 [L]
Just make sure you clear your cache before testing this.
How do I strip a part of the url? I do not know much about htaccess or apache.
I would like to strip www.mysite.com/page=services to www.mysite.com/services for example.
What exactly do I need to put in the .htaccess file in order to achieve this, and would that work for other pages as well?
Thanks.
I tried this for one of my sites lately and came up with this and for me it works fine,
this goes into the .htaccess file:
RewriteEngine On
RewriteRule ^([A-Za-z]+)/?$ index.php?page=$1 [NC,L]
then if you would write yoursite.com/pagename it would send yoursite.com/index.php?page=pagename to your php.
You will link to the page like so: yoursite.com/pagename
it wont change yoursite.com/index.php?page=pagename to yoursite.com/pagename in the adress bar after you send it.(if that makes sense :) )
I hope this is what you are looking for...
I think you mean your original URL to be www.mysite.com/index.php?page=services and not www.mysite.com/page=services
Also, you probably mean the opposite, you should switch www.mysite.com/services to www.mysite.com/index.php?page=service
Anyway, to change www.mysite.com/services to www.mysite.com/index.php?page=services then you need .htaccess, and the rule for that would be RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L]
As suggested earlier, you should read about .htaccess, regex, and rewrite rules. Best resource is the apache documentation here: http://httpd.apache.org/docs/2.2/howto/htaccess.html
I read your comment earlier that you need the opposite, I am not sure why you need that, because the whole idea of URL shortining is to make easy-to-remember URLs in addition to some security concerns that can be resolved. The URL is the first thing that is sent to load your webpage, then .htaccess changes it to some form undrstandable by PHP then PHP deals with get parameters for example.
I have currently hit a bit of an issue with redirecting users with .htaccess and was wondering if anyone could help me.
1. Background:
I currently have a rather long domain name for the sake of this question lets refer to it as mylongdomainname.com now on this domain I have a subdomain that I use to host files, pictures etc to show friends or share with people this is files.mylongdomainname.com
now obviously the URL can get quite long as I have different directories and files. so to help reduce a bit of space I purchased another short domain, lets refer to this as small.me now what I want to do is use .htaccess and a simple PHP file to redirect small.me to files.mylongdomainname.com and pass on a file reference.
Example:
small.me/pictures/example.jpg should redirect to files.mylongdomainname.com/pictures/example.jpg
2. The problem
Basically I am unsure on the exact rewrite rule I would need to acomplish this. obviously I need a rule that will allow anything after small.me/ to be sent with the GET method to the index file which would then redirect the user accordingly. So that means that the rewrite rule will have to allow all letters, numbers and valid file name symbols to be used. I'm sure it's simple but after looking at a few tutorials and mod_rewrite help sites I still have no idea how to accomplish this.
3. The Code
.htaccess
RewriteEngine on
RewriteRule ^$ index.php?file_location=$1 [L]
obviously wrong
index.php
<?php
//Get the requested files location.
$file_location = $_GET['file_location'];
//Redirect the user to the file.
header('refresh:2; url=http://files.mylongdomainname.com/' . $file_location);
?>
4. Notes
I am aware I could just use a URL shortener, but because I am awkward I would rather it just went through my own domain, so please don't comment or answer telling me to use a shortener or to use a service like dropbox.
So can anybody help me by providing the right rule? Any help is much appreciated.
In .htaccess you can simply use:
RewriteRule ^(.*)$ http://small.me/?$1 [L]
No need for the PHP file if that's all you're trying to do.
Assuming you want this:
Picture to be shared: http://files.mylongdomainname.com/pictures/me/troll.jpg
Desired URL: http://small.me/pictures/me/troll.jpg
Remove the PHP file, just Place this in small.me's htaccess:
RewriteRule ^\/?(.*)$ http://files.mylongdomainname.com/$1 [NC,L]
The following in your .htaccess file should be all you need (no PHP file needed):
RewriteRule ^(.*) http://files.mylongdomainname.com/$1 [RL]
For more information and examples see the mod_rewrite documentation
I have a small question to ask. Is it possible, via php or htaccess, to change a url like: miodominio.com/users.php?idu=x into something like miodominio.com/username ?
I want it to be Facebook style...Where "username" is the username chosen by idu = x.
There are multiple ways to solve this problem, but here's one that always suits my needs.
Guide all your URL requests through the index.php first and resolve the request in your PHP code second.
1) Use an .htaccess file to direct all URL's through index.php. You'll find one way here by the CodeIgniter framework and a more advanced explanation here. I recommend the CodeIgniter .htaccess guide first if you're inexperienced with .htaccess.
2) Second, use the $_SERVER variable in PHP to extract the URL. Probably with the help of the $_SERVER['REQUEST_URI'], you'll find '/username/' which you can then use to extract the user's data and serve it to them.
Good luck and beware of URL injections using this method.
You need to use apache's mod_rewrite for this. It can translate miodominio.com/username to miodominio.com/users.php?idu=x. There are some good guides about this which are easy to find with Google.
You can try to use this mod_rewrite pattern (add it to the .htaccess):
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?idu=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?idu=$1
you have to write a clean URL in your .htaccess file like :
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$ users.php?idu=$1
Put the following in your .htaccess
RewriteEngine on
RewriteRule ^([a-z0-9_-]+)/?$ /users.php?idu=$1 [NC]
The [NC] will make it case-insensitive, if you accept only lowercase username, remove the [NC] from the last.
My current code is something like this
store.php?storeid=12&page=3
and I'm looking to translate it to something like this
mysite.com/roberts-clothing-store/store/12/3
and something like this:
profile.php?userid=19
to
mysite.com/robert-ashcroft/user/19
I understand that it's best to have the SEO-friendly text as far left as possible, ie not
mysite.com/user/19/robert-ashcroft
(what stackoverflow does)
I can't figure out how to do this in apache's mod_rewrite.
Actually, you may have to think "upside-down" with mod_rewrite.
The easiest way is that to make your PHP emit the rewritten mysite.com/roberts-clothing-store/store/12/3 links.
mod_rewrite will then proxy the request to one PHP page for rewrite.php?path=roberts-clothing-store/store/12/3 that will decode the URL and sets the arguments (here storeid and page)
and dynamically include the correct PHP file, or just emit 301 for renamed pages.
A pure solution with mod_rewrite is possible, but this one is much easier to get right, especially when you don't master mod_rewrite.
The main prob could be with the overhead that might be significant but is the price of simplicity & flexibility. mod_rewrite is much faster
Update:
The other posts do answer the question, but they don't solve the typical duplicate-content problem that avoided by having canonical urls (and using 301/404 for all those URLs that seems ok, but aren't).
Try these rules:
RewriteRule ^[^/]+/store/([0-9]+)/([0-9]+)$ store.php?storeid=$1&page=$2
RewriteRule ^[^/]+/user/([0-9]+)/ profile.php?userid=$1
But I wouldn’t use such URLs. They don’t make sense when you think of the URL path as a hierarchy and the path segments as their levels.
RewriteRule ^roberts-clothing-store/store/([^.]+)/([^.]+)$ store.php?id=$1&page=$2
RewriteRule ^robert-ashcroft/user/([^.]+)$ profile.php?userid=$1
Then you can just use RewriteRule directive in a .htaccess like:
RewriteRule roberts-clothing-store/store/(\d+)/(\d+)$ store.php?storeid=$1&page=$2 [L]
See http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html for help, or google.
My approach is to make the .htaccess as easy as possible and to do all the hard work in PHP:
RewriteRule ^(.*?)$ index.php?$1
This basically means to take everything and reroute it to my index.php file (in css/javascript/image directories I simply use "RewriteEngine off" to grand access to these files). In PHP I than just split("/", $param, 5) the string and run a foreach() to check all the parameters. Encapsulated in a nice function this works fine for me.
Update:
For this easy case I highly recommend the use of explode() instead of using split(), because explode() doesn't come with the overhead by using regular expressions.