how to use different url structure in php ?
I want to know to handle url like this
www.example.com/10/2011
instead of
www.example.com/?m=10&y=2011
Maybe it will be better to do with server configuration - .htaccess (in Apache) or web.config (in IIS). Create rewrite rule from ^(\d+)/(\d+)$ to /?m=$1&y=$2. For example, in Apache it will look like:
RewriteEngine On
RewriteBase /
RewriteRule ^(\d+)/(\d+)$ /?m=$1&y=$2
this can be achieved through apache's mod_rewrite mechanism. create a .htaccess file in your webroot with the following contents:
RewriteEngine on
RewriteRule ^([0-9]+)/([0-9]+) ?m=$1&y=$2 [NC]
similar mechanisms exist for other web servers.
You should use Rewrite(google it) engine in Apache or nginx.
Something like this exists in ISS too.
This is not achieved by PHP, but by some other "component". E.g. the Apache web server has a module (the rewrite engine) that can rewrite the URL so that you can use URL like www.example.com/10/2011 and it translates them into www.example.com/?m=10&y=2011 so that in your PHP code you can use $_GET['m'] and get the month. Rewrite engine for Apache (for other webserver similar things may exist)
One easy method is something like this:
In your .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
What this does it redirects everyhting to index.php if its not a physical file or dir.
Then you configure your index.php to handle this request.
Example
www.example.com/10/2011
will become $_GET['10/2011'];
so you can use data in your script.
Related
I have a page with URL like this:
https://something.com/paste/log.php?log=H7nSEIPaVr
(By the way my homepage is:
https://something.com/paste/index.php
)
I want to make it work by just giving the log parameter like this:
https://something.com/paste/H7nSEIPaVr
And it would redirect me to the original url.
(or it would be better if it just give me the paramter - ?log=xxx)
I have a .htaccess file, but it is not working for me.
(rewrite is enabled, so the htaccess is working well)
RewriteEngine on
RewriteRule /paste/^([a-zA-Z0-9]*$)/? /log.php?log=$1 [QSA]
This probably is the rule you are looking for:
RewriteEngine on
RewriteRule ^/?paste/([a-zA-Z0-9]+)/?$ /paste/log.php?log=$1 [L]
That rule needs to be implemented in the central http server's host configuration. Or, if you do not have access to that, you can instead use a distributed configuration file (typically called ".htaccess") which has to be located inside the http server's DOCUMENT_ROOT folder.
An alternative would be to place that variant in a distributed file inside the /paste folder:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)/?$ log.php?log=$1 [L]
I have sub-domain likedemos.testing.com and i would like to get my url like
demos.testing.com/how-do-i-find-the-difference-between-two-dates-using-jquery
I have index.php file in demos.testing.com root folder and i reading the first parameter as
$request_uri = substr($_SERVER["REQUEST_URI"], 1);
When i request site like : demos.testing.com/how-do-i-find-the-difference-between-two-dates-using-jquery it is giving following error :
The requested URL /how-do-i-find-the-difference-between-two-dates-using-jquery was not found on this server.
Please tell me how can rewrite url in .htaccess file.
.htaccess is an Apache specific configuration file, and more over depending on your main Apache configuration, it may or may not even be parsed. To get an answer, you need to properly tell us what Web server you are using (is it Apache? If not .htaccess won't work and you need to write your rewrite rules in some other Web-server specific way). If Apache, do you have AllowOverride set?
Assuming all of this is true (you use Apache and it is set to read .htaccess), try this rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
This basically rewrites everything which is not a real existing file or directory to index.php.
As I said, depending on your setup, this may not work. If it doesn't, please tell us more about your setup (which server, what PHP SAPI, etc.).
I like the way CodeIgniter cleans URLs for sites, however, I have a site that is too involved to start over using the CI framework (at least for now), and I don't need the depth CI provides, I only have one level deep.
Is there an easy way to do this easily using straight PHP?
index.php?id=2454
index.php/2454/
NOTE: I need a straight PHP solution because the server is Windows and .htaccess is not setup to work.
If you use mod_rewrite in apache you can allow your application to dispatch requests as you want.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Say your server is located # http://coolguy.com and a users accesses http://coolguy.com/mycleanurl/
With the above rewrite rule in your .htaccess or apache configuration you can intercept which url is being accessed via $_SERVER['REDIRECT_URL'] and send it off to the specific code point you want.
The "RewriteCond" directives i have in there are used to ignore this rewrite rule if there exists a file directly at the location the user has specified, this is handy for static assets like CSS and images where you dont want to have to dispatch these requests yourself.
Check out $_SERVER['PATH_INFO'] - it returns anything trailing the script filename but preceding the query.
For example, in the URL:
http://www.domain.com/index.php/var1/var2
$_SERVER['PATH_INFO'] would contain /var1/var2
You could then write a function in your __construct() or init(), etc, to parse the path (e.g. explode("/", $_SERVER['PATH_INFO'])) and use the resulting array as variables.
Are you using Apache? If so, look into files called .htaccess. They rewrite the URL when they're stored in your directory. So if you put an .htaccess file in your web root with
# .htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/www/
RewriteRule ^(.*)$ /www/$1 [L]
then when a person goes to the URL yourdomain.com/page1, it will actually retrieve the resource yourdomain.com/www/page1 on your server, but their browser won't see the www.
So, for very simple URL rewriting you can use this to hide the ?var=val&var2=val2 crap in the URL
Information on the rules is here
If your using PHP on windows and want to do URL Rewriting you have two choices:
Url Rewrite (which allows you to use rewrite rules in the web.config) http://www.iis.net/download/urlrewrite - Open the site in IIS Manager and select "Url Rewriting", then add your rules.
Buy Helicon-APE or similar (which allows you to use a native .htaccess file) http://www.helicontech.com/ape/ - we use this on our shared windows hosting servers with great success
Generally a url that looks like this:
http://www.domain.com/product.php/12/
will open up product.php and serve the /12/ as request parameters, which then my PHP script can process to pull out the right product info. However when I migrated this whole site, after developing it, to a new server, I get a 404 error, because on that server it's not defaulting to the mother directory/file in case of an absence of requested directories.
I vaguely remember learning that this is generally a common apache function but I can't seem to recall how to set it up or how to manipulate it.. if there's an .htaccess method to achieve this that would be great.
What you're referring to is mod_rewrite. The official docs for it are here: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
You would configure it either in your VHost definition (recommended) or in an .htaccess file.
Assuming that you want to map all requests to a resource that Apache cannot serve (such as files that don't exist) to products.php you can use the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /products.php?request=$1 [NC,L]
You can then use $_GET['request'] to get the path requested and take it from there, depending on what you want to do. I'd normally recommend letting mod_rewrite handle parsing the request and passing the proper attributes to your PHP, but if you're not familiar with mod_rewrite it's probably easier to do it in your PHP.
you can use mod rewrite engine to map this to
http://www.domain.com/product.php?arg=12
Mod rewrite details: http://forum.modrewrite.com
Sample:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^files/([^/]+)/(.+) files.php?app=$1&file=$2 [NC]
this rewrite rule will map any request containing files/firstrPart/secondpart to the script files.php
everything between the first and second slash after files will be passed as parameter app and the rest as file
Basicly you define a regex with some subpaterns and state which script should really be called.
You cna refer to the subpatterns with $n where n is the 1 based index of the pattern.
Have fun.
NOTE this is a extreme simplification of mod rewrite. Please do some research before you use it because this might go terribly wrong...
The directive you're looking for is "AcceptPathInfo on". mod_negotiations MultiViews feature would also give you the option of not including the ".php" which is another common one people abuse mod_rewrite to do.
I want to my adress look like this: www.example.com/112112/example
Where 112112 is a data, that I want to work with in a php script. How to do this? For some reasons, I dont want to the adress look like www.example.com?id=112112
You can use url rewrite to achieve the binding from www.example.com/112112/example to www.example.com?id=112112
For Apache webserver, you will find here a guide: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
This is about mod_rewrite and .htaccess-files, add a .htaccess-file to your webserver root and add this piece of code:
RewriteEngine On
RewriteRule ^(.*)/ index.php?id=$1
The easiest way to do this is if you're on a system using the Apache web server, along with mod_rewrite. To get the desired effect, you'd put a .htaccess in your directory with something like this:
RewriteEngine on
# Dont rewrite files or directories which actually exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite for example.com/100/example to example.com/page.php?id=100
RewriteRule ^([^/]+) page.php?id=$1
You can have lots of rules to cover different possibilities.