Im trying to pass some variables from the URL to the PHP script. Now I know that www.site.com/index.php?link=HELLO would require $_GET['link'] to get the variable "link". I was hoping there are other ways to do this without the variable.
For instance I want a link structure like this: www.site.com/HELLO. In this example I know that I have to create a Directory called Hello place an index file and it should work but I don't want to create a directory and Im hoping there's a way to "catch" that extra part after the domain. I'm thinking of creating a custom HTTP 404 Page that will somehow get the variable of the not found page, but I don't know how to get the HTTP 404 error parameters. Is there another simpler way to get a variable without the use of the extra ?link= part? I just want it to be a structure like this www.site.com/HELLO.
What you want is URL rewriting. You don't mention what kind of web server you're using, so I'll assume it's Apache.
If you have mod_rewrite enabled on your web server, this can easily be accomplished by creating a .htaccess file in your document root with the following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1
This will make all requests - that match the regular expression [a-zA-Z0-9]+ - get forwarded to index.php. For instance, if you try to access domain.com/hello, PHP would interpret this as trying to access index.php?request=hello.
You can read more about this in the Apache HTTP Server manual about mod_rewrite.
In which case, you normally use .htaccess to alter the URL in some form. For instance:
RewriteEngine On #Enable Rewrite
RewriteCond %{REQUEST_FILENAME} !-f #If requested is not an existing file
RewriteCond %{REQUEST_FILENAME} !-d #If requested is not an existing directory
RewriteRule ^(.*)$ /index.php?link=$1 [L] #Preform Rewrite
Which will make www.site.com/hello to www.site.com/index.php?link=hello. This change is invisible to the user (he will still see www.site.com/hello as an address). Be advised that it may cause trouble if you try using relative paths with CSS/JavaScript files.
You need to use URL rewriting to do this. If you're using Apache: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
you can do that with a mod-rewrite structure.
Here's a pretty good tutorial on how to set it up with php/apache.
http://www.sitepoint.com/guide-url-rewriting/
Related
I have a script located at http://www.foo.bar/script and would like to use virtual path names to send data to that script. For example, http://www.foo.bar/script/this/is/a/path would pass "this/is/a/path" to /script. I would like to do this without changing the URL the user sees.
I've already gotten this to work with Apache mod_rewrite using something similar to what was suggested here htaccess mod_rewrite multiple paths to query string. What I have not been able to do is to pass the path to the script without changing the URL the users sees. So, a user that visits http://www.foo.bar/script/stackoverflow/rocks, the script residing at /script would receive /stackoverflow/rocks as a query string or URI but the URL would not change. I know this is not uncommon, and perhaps I'm using the wrong terminology when searching for an answer. Thank you for considering.
One idea someone suggested:
RewriteEngine On
RewriteBase /
RewriteRule "script/(.*)" "https://www.foo.bar/script/?data=$1" [R,L]
However, the above changes the URL in the browser. I don't want to expose "?" on the query string.
I don't really have the exact code you need but I have something I use that might just help you. The example above using rewrite_module will rewrite any URL with .php and will also accept the page name without the .php and redirect to a pageName.php witch is close to what you want, the name the users write, redirecting to the script you want. Additionaly it also offer some protection against cross scripting at the end of the URL.
<IfModule rewrite_module>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{QUERY_STRING} ^(.*?)(?:%3C|%3E|<|>)(.*)$
RewriteRule ^((.*)((?!([.]*p*h*p))\w+)) /$1?%1%2 [L,R=302,NE]
</IfModule>
I'm trying to figure out a way to load a template php file AS a requested url/filename:
I have a directory with hundreds of pages, and would like to be able dynamically generate them from a template file rather than have hundreds of files containing the same code. Ideally there would be one php file that loads content from MySQL using basename(), where every url requested from a particular directory would open /gallery/template.php AS the requested url, such as /gallery/example.html.
I feel like this can probably be done using .htaccess and mod-rewrite, but I haven't found an example of it in action. I'm trying to avoid using GET, but if there is a better way to achieve this effect, I'm open to suggestions. Thank you.
You might want to use AJAX as a method for accomplishing what you want. Are you familiar with AJAX? If you post more details about your specifics and desired outcome, perhaps we can help you further.
Review these simple AJAX examples, and see this video resource.
Take a look at $_SERVER['PATH_INFO'] on the $_REQUEST manual page. Using that, you can get something like
http://example.com/index.php/path/to/your/template
to call index.php passing along the entire /path/to/your/template as a string in $_REQUEST['PATH_INFO'].
And in your .htaccess file, you can do the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
(rewrite rules taken from here. I haven't done the mod-rewrite part myself, so I can't guarantee this will work).
This will take something like http://example.com/path/to/your/template and have the server treat it as http://example.com/index.php/path/to/your/template, making it pass everything to your index.php file.
After that, how you load your templates is up to you.
I haven't test it, but here is an example:
RewriteEngine On
RewriteRule ^gallery/.*/?$ gallery/template.php [NC,L]
String 'gallery' must be in the requested URL.
Basically, you are wanting to execute a front controller pattern. If everything in your gallery subdirectory needs to be redirected (i.e. there are no images files or subdirectories which don't need to be routed to template.php), you can do a simple RewriteRule to achieve this. Just place this in .htaccess or your apache .conf file
RewriteEngine On
RewriteRule ^/?gallery/.*\.html$ /gallery/template.php [QSA,L]
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
im new to Mod_rewrite but problem is it ruins all my ajax and php
i use windows.location object to get current location for javascript example:
var str = location.pathname;
str=str.slice(0,str.lastIndexOf('/'));
problem that this never works since i used mod_rewrite.
my .htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^cars/([^/]+)/?$ profiles.php?carname=$1 [L]
can some body lead me to way to fix this. ?
also i face same problem with lading all imgs and css/.js but i heard i can write condition on .htaccess to only redirect .php !,
I need a way to find base address for ajax calls, and other way to find base address for php.
the problem is that you are redirecting all your url to a php file and it breaks all the other assets, including css and js.
Solution:
Use a rewrite condition. this way it only redirects if the file or directory doesn't exist.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cars/([^/]+)/?$ profiles.php?carname=$1 [L]
the first line is : if file doesn't exist
the second is: if directory doesn't exist
the there will be a redirect.
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.