Forum Moderators: phranque
Example URLs of my site would be:
[domain.com...]
[domain.com...]
I would like them to appear:
[domain.com...] (or .html)
[domain.com...]
But here's where my bi-directional question comes in: Is it possible to use mod-rewrite to have my links within my document appear as:
<a href="home.php">home</a>
instead of current:
<a href="index.php?page=home">home</a>
Or, because of the $_GET'[page] factor in PHP is that not possible? Here's my PHP dynamic page code if that helps:
<?php
if(isset($_GET['page'])) {
include("{$page}/index.php");
print($page);
}
else {include('home/index.php');
}
?>
Would this need to be changed to accomplish what I'm trying to do?
BTW, I tried tackling this myself but didn't get very far. Because each page variable changes, I got confused:
RewriteEngine on
RewriteRule ^index\.php$ newlink.php [R=301,L]
mod_rewrite takes the URI specified in an incoming HTTP request and either generates an external redirect or modifies the local path for use within the server, depending on what you specify in the RewriteRule. This takes place on the front-end of HTTP request processing -- before any script is called or any content is served. mod_rewrite cannot be used to change the links within pages already generated by your scripts before the server sends those pages to the requesting client; It only works on requested URIs.
To achieve search-engine friendly URIs, do the following:
Jim
[domain.com...]
[domain.com...]
And make them appear:
[domain.com...] (or .html)
[domain.com...]
How would I do that? TIA
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.*)\.htm /?page=$1 [L]
However, I'm curious if there is a way to specify the RewriteRule to work whether the user adresses
mydomain/link.htm -OR-
mydomain/link/ -OR-
mydomain/link
without having to write a seperate rule for each?
And what's the difference between [NC] and [QSA] after the rule?
I realize I can make a string for the rewrite to identify and only rewrite on it:
RewriteRule ^index\(.*)\.htm /?page=$1 [L]
however, I would prefer not to have every page appear this way in the address bar:
domain.com/index/home.htm
domain.com/index/about.htm
Is there a way to have mod-rewrite only take the information in the first subdirectory:
domain.com/home.htm
and complete the rewrite, yet not attempt a rewrite if the address is deeper than one subdirectory:
domain.com/about/location/map.htm
So, "search" for the slash, and don't redirect if there is one following any subdirectory name and before any filename. Just add a RewriteCond ahead of your rule:
RewriteCond %{REQUEST_URI} !^/[^/]+/
Ref:
Apache mod_rewrite Documentation [httpd.apache.org]
Apache URL Rewriting Guide [httpd.apache.org]
Regular Expressions Tutorial [etext.lib.virginia.edu]
Jim
I need to rewrite the url as follows, regardless of it's length (number of subdirectories):
[domain.com...] REWRITE TO:
[domain.com...]
but also work if there are more subdomains: (regardless of the number of subdomains)
[domain.com...]
[domain.com...]
OR
[domain.com...]
[domain.com...]
Current rule:
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.*)\.htm /?page=$1 [L]
I tried to incorprate parts of the RewriteCond that jd gave me, but I couldn't get any variation to work.
RewriteRule ^[(.*)+/](.*)\.htm $1/?page=$2 [L]
I tried this and 30+ other combinations. Please help?
Based on that, try something like this:
RewriteRule (.*/)?([^/]+)\.htm$ /$1index.php?page=$2 [L]
mod_rewrite must evaluate this pattern from right to left to match it, so it will be a bit slow. Also, this code is intended for use in .htaccess. For use in httpd.conf, leave off the slash in front of $1.
Jim
Take a look at the "design" of your friendly URLs, and consider adding a "key" for regex to look for. This is a bad example, but something like:
mydomain.com/fee/fie/foh/fum/start/somepage.html
is easy to parse if you tell regex to put everything up to "start" in $1, and everything after "start" in $2.
The key shown above as "start" needs to be a unique word, one that won't conflict with a real directory name, and also one that won't look silly or suspicious to visitors.
Another alternative would be to move the whole local filepath into the query string, and let the script take care of it.
Just some thoughts.
Jim