Forum Moderators: phranque
My site is www.example.com
The links are like this
[example.com...]
Where 15 is a record in our database.
In order to optimize for search engines, I want to make
I want be able to use links like this on my pages
[example.com...]
(or .php or any similar variation that would be search engine friendly.)
and for that to pull up the former dynamic page so that it looks static to the search engines. The record numbers are not all two digits.
<snip>
Now I've been looking at all the examples on this site and others. I have used a variety of examples in my .htaccess file. I'll list a few here.
This code is in my .htaccess file now.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule /about(.*).htm$ /aboutus.php?oid=$1
I have also tried without the fist line, without the third line, etc. The dynamic page is in the root directory.
I have also tried adding [T=application/x-httpd-php]
to the rewriterule line.
I've also tried many other variations of this code including ...
RewriteRule ^about([^.]+).php$ aboutus.php?oid=$1
and
RewriteRule ^about([^.]+).htm$ aboutus.php?oid=$1
Since I don't really know what the heck I'm doing or what all thes carrots. plusses, and dollar signs mean, I don't know if I'm missing something in the code, if I have the links wrong, if there's something in my php code that's conflicting, or if my host has something funky going on.
Thanks
[edited by: jdMorgan at 3:06 am (utc) on Oct. 18, 2004]
[edit reason] No URLs, please. See TOS. [/edit]
Welcome to WebmasterWorld!
We're going to need a definition of "It doesn't work" to be able to be of much help to you.
For help with carats and dollar signs, as well as mod_rewrite, see the references cited in our charter [webmasterworld.com].
Jim
Placed in the Web root directory .htacess, this code rewrites from example.com/about<digits>.html to example.com/aboutus.php?oid=<digits> :
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^about([0-9]+)\.html$ /aboutus.php?oid=$1 [L]
Periods, plus signs, etc. are reserved characters that have a function in the regex and if you need to match one of the reserverd chars, you must escape them with a back slash (\)
[6]Welcome to Webmaster World, zipadyduda![/6]
It turns out the problem was with my .htaccess file with the host and they fixed it and your code works now.
If I want to do other similar dynamic pages, I can do more lines like the current rewrite line following it and moving the [L] to the last line?
For example
#Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^about([0-9]+)\.html$ /aboutus.php?oid=$1
RewriteRule ^reviews([0-9]+)\.html$ /reviews.php?oid=$1
RewriteRule ^services([0-9]+)\.html$ /services.php?oid=$1 [L]
correct?
Everything is working fine now but I'm confused about how this is working. I'm not understanding how the variables get passed using this method.
What I mean is, when a link of
[example.com...]
is entered and goes through
RewriteRule ^about([0-9]+)\.html$ /aboutus.php?oid=$1
is the rewrite module grabbing the numerical value 15 and rearranging the url?
mod_rewrite substitutes the URL or URL-path on the right end of the RewriteRule for the requested URL, including any back-references, if the requested URL matches the pattern of the RewriteRule and all RewriteConds are met.
Jim
One thing I read is to put the .htaccess file in the subdirectory, But I thought this might conflict with the existing .htaccess file.
Another thing I thought would be if I could use a second RewriteBase command. Is it possible to change the rewritebase in the code?
eg.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^about([0-9]+)\.html$ /aboutus.php?oid=$1 [L]
RewriteBase /folder/
RewriteRule ^more([0-9]+)\.html$ /more.php?oid=$1 [L]
Then there is the possibility of coding the rewriterule to accomodate the subdirectory, but my coding is weak and I'm not sure of the syntax.
so something like this?
RewriteBase /
RewriteRule ^about([0-9]+)\.html$ /aboutus.php?oid=$1 [L]
RewriteRule ^folder/more([0-9]+)\.html$ /more.php?oid=$1 [L]
Thanks
You can have an .htaccess file in each directory of your site if you like. The higher level (root) .htaccess file is processed first. Unless it contains a rewrite or some other directive that prevents it, each .htaccess file along the filepath to the requested resource will then be processed in order.
Some people like to use an .htaccess file in each subdirectory because it is more efficient; The directives in a given .htaccess file need only be processed for requests to its subdirectory. On the other hand, some people like to put all of their directives in the top-level .htaccess file, so as to have "centralized" configuration control and easier maintenance. Others use a mix of the two techniques, as dictated by server load and convenience.
> Another thing I thought would be if I could use a second RewriteBase command. Is it possible to change the rewritebase in the code?
I've never tried that, but RewriteBase is not the correct way to handle URL-visible subdirectories, and most probably won't solve your problem, either.
Your code
RewriteBase /
RewriteRule ^about([0-9]+)\.html$ /aboutus.php?oid=$1 [L]
RewriteRule ^folder/more([0-9]+)\.html$ /more.php?oid=$1 [L]
You may want to use
RewriteRule ^folder/more([0-9]+)\.html$ [b]/folder[/b]/more.php?oid=$1 [L]
Either way, the syntax is correct, it just depends on where the more.php script is located.
Jim
I tried to put this into my .htaccess file (on a shared server)
# activate rewrite engine
RewriteEngine on
RewriteBase /
# test rule
RewriteRule ^/somepage.htm$ /someotherpage.php5
Brings up a 404 error. Yes, I could look in the log, but I get them only once a day ....
.. and
if I comment everything but the "RewriteEngine on" I still get the 404 error. Could that be a problem with the server setup - or am I missing something?
#activate rewrite engine
RewriteEngine on
#RewriteBase /
# test rule
#RewriteRule ^/somepage.htm$ /someotherpage.php5
Thanks for any help, nerd.
You may be missing a custom 500-Server Error page. If you have removed the RewriteRule, and yet still get a 404, then one possible mechanism is this:
Server detects that your have not enabled FollowSymLinks and invokes 500-Server Error.
Server attempts to find custom 500-Server Error page and fails.
Server invokes 404-Not Found error handler.
This would apply only if you or your host has configured an ErrorDocument directive for defining custom error handler pages.
Jim
100% correct!
My acct wasn't enabled for mod rewrite - now that I have the log I also can see the 500-errors. (cgi-something)
And yes, I had a "." without a "\" in the rule I posted here, but that was simply a typo.
Meanwhile everything's up and running (the site's not mine but for a non-profit organisation) - and everybody is happy now.
Thanks, guys!