Forum Moderators: phranque
So, if I have a URL that would appear like this:
[somesite.com...]
How would I rewrite it so that it just went:
[somesite.com...]
Any help?
change this...Well, if you are using Apache for your web server, you are going to want to get more familiar with mod_rewrite [httpd.apache.org] and the URL Rewriting Guide [httpd.apache.org]. The Apache forum [webmasterworld.com] here at WebmasterWorld is also a better place to post this type of question for future reference. If you are using Apache, here is an example that you could use in a per-directory override file (.htaccess):
http://somesite.com/pagename
to this...
http://somesite.com/index.php?page=pagename
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/([^\/]+)/?
RewriteRule .* index.php?page=%1 [L]
RewriteCondand
RewriteRuledirectives are processed and the user is sent the PHP
$_GETurl you specified in the
RewriteRule.
By calling this function at the top of a page, the array $values contains the URL that was used to access the page, divided by slashes.
You can then login to your server and create a link (do a man ln for more info) to a static "file" in your web directory, for example "directory", from your php script.
When a user accesses www.yourdomain.com/directory/widget/1.php
your php script will be called and $values will contain the the values widget and 1, along with the domain name and "directory". You can then use these values in the script with minimal modification by overwriting the needed GET vars.
This solution is easy, IMHO, if you are better with PHP than apache.
<?php
require 'head.inc';
if (isset($_GET['class'])) {
$class="";
}
if (!isset($_GET['page'])) {$content= 'page1.inc';}
else {$content= $_GET['page'].'.inc'; $class="tc";}
include 'containers.inc';include $content;
include 'footer.inc';
?>
a user selects a link in the menu (the menu is included in the containers.inc file)
This is a link example:
<li><a href="index.php?page=support" id="spbtn">Support</a></li>
http://somesite.com/index.php?page=support
All I want to do is figure out the easiest way to have the url display as
http://somesite.com/support
John
Ya gotta trust coopster on this one. Just try it out and see how it works.
First change your menu links to use the directory stucture you want the user to see.
<li><a href="http://www.somesite.com/support" id="spbtn">Support</a></li>
Now make a simple text file called .htaccess with the following in it and put it in the same directory.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/([^\/]+)/?
RewriteRule .* index.php?page=%1 [L]
Now click on the link.
You'll see [somesite.com...] but the script will actually use [somesite.com...]
It'll work like a charm.
RewriteEngine on
RewriteCond %{REQUEST_URI}!^/index\.php
RewriteRule ^([^/]+)/? index.php?page=$1 [L]
You may also wish to exclude other filetypes from being redirected, depending on how your site is configured. For example, to exclude jpeg image requests from being redirected, you could use:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} !\.jpg$
RewriteRule ^([^/]+)/? index.php?page=$1 [L]
I tried copying off of JD...
RewriteEngine on
RewriteCond %{REQUEST_URI}!^/index\.php
RewriteRule ^([^/]+)/? index.php?page=$1 [L]
...but now $1 contains "index.php" -- it's not supposed to, I reckon. Little help?
RewriteEngine on
RewriteCond %{REQUEST_URI} !/index\.php
RewriteRule ^([^/]+)/? index.php?page=$1 [L]
jd Morgan said: Note that slashes in the pattern need not be escaped for use in mod_rewrite.
hehe, yep, I even missed one anyway; too much PHP regexing lately ;-)
include $content;
include 'footer.inc';
?>
This way you don't have to mess around with apache.
The reason that this function needs to be done at the server level and not via php is that the problem to be solved is this: How do I deliver a request for a "page" to my php script so that I can process it? The reason php won't work in this case is that it can't work if it has not been invoked yet. IOW, you have to deliver the request to the php script before it can do anything.
This can be done using a "smart" function such as mod_rewrite to selectively rewrite incoming URLs to the script, or simply by declaring the script as the handler for all http requests. But either way, it requires a server-level directive.
Jim
That command would look like this
ln script.php directory
I am making my first modrewritten-URl site. So far ive used what I found in this post sucessfuly on one variable.
ive used this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/index\.php
RewriteCond %{REQUEST_URI} !^/admin
RewriteCond %{REQUEST_URI} !^/badev
RewriteCond %{REQUEST_URI} !\.jpg$
RewriteCond %{REQUEST_URI} !\.css$
RewriteCond %{REQUEST_URI} !\.gif$
RewriteCond %{REQUEST_URI} !\.js$
RewriteRule ^([^/]+)/? index.php?page=$1&cat=$2 [NC,QSA,L]
and the urls processed:
www.example.com/page.
Thats as far as it went for me. Please note in Rewriterule ive tried to rewrite this:
www.example.com/page/cat
to this:
www.example.com/index.php?page=somepage&cat=somecategory
But when i klick the actual link that was supposed to take me from
www.example.com/somepage to www.example.com/somepage/somecategory
i get rather: www.example.com/somecategory.
My try was:
RewriteRule ^([^/]+)/([^/]+)? index.php?page=$1&cat=$2 [NC,QSA,L]//doesnt work as desired (brakes the page)
Ive searched everywhere and tried bunch of stuff but cant seem to get the upper .htaccess code to work for more than one variable. Can someon please make an example or at least a hint would be appreciated.
best
REP