Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite make dynamic look static not working

make dynamic look static

         

zipadyduda

8:05 pm on Oct 17, 2004 (gmt 0)

10+ Year Member



I know this is on here in several places and it's a pretty common thing but It's not working for me and I'm pulling my hair out trying to figure out why. And there are a couple of things about this I don't quite understand.

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]

jdMorgan

3:17 am on Oct 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



zipadyduda,

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

zipadyduda

6:24 am on Oct 18, 2004 (gmt 0)

10+ Year Member



With that code in the .htaccess file, the static link should pull up the dynamic page right?

As it is, I get page not found.

jdMorgan

12:42 pm on Oct 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your server error log should show the new URL if a 404-Not Found error is encountered. Examining that should give a good idea as to what might be wrong.

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]

Jim

Birdman

12:53 pm on Oct 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And if you look closely at JD's example you'll see that the error was the fact that you didn't escape the period(.)

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]

zipadyduda

6:35 pm on Oct 18, 2004 (gmt 0)

10+ Year Member



Thank you for your response.

This new code gives the same results.

I don't think I have access to the server error log file mentioned. I'm trying to contact my host about it.

zipadyduda

7:33 am on Oct 19, 2004 (gmt 0)

10+ Year Member



Thank you so much for your help.

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?

Birdman

12:05 pm on Oct 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You would want to use the [L] flag on all those lines. Glad to hear you got it working properly. You should really take the time to read the documentation so you can get a better understanding. Click the charter link on this page and there you will find some great resources.

Regards,
Birdman

zipadyduda

8:41 pm on Oct 19, 2004 (gmt 0)

10+ Year Member



I have found this page to be extremely helpful.

<snip>

Perhaps you may consider adding it to your charter.

[edited by: jdMorgan at 10:01 pm (utc) on Oct. 19, 2004]
[edit reason] NFP .org and .edu links only, please. [/edit]

zipadyduda

10:44 pm on Oct 19, 2004 (gmt 0)

10+ Year Member



Sorry for the faux pas, but the information thiere is darn good.

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?

jdMorgan

12:01 am on Oct 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look for the term "back-reference" in the mod_rewrite documentation (specifically, RewriteRule and RewriteCond), and all will become clear.

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

zipadyduda

3:14 am on Oct 21, 2004 (gmt 0)

10+ Year Member



I want to also rewrite files in a subdirectory.
I have read different things abou this.

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

jdMorgan

3:58 am on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> One thing I read is to put the .htaccess file in the subdirectory, But I thought this might conflict with the existing .htaccess file.

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]

would rewrite yourdomain.com/folder/more123.html to yourdomain.com/more.php?oid=123

You may want to use


RewriteRule ^folder/more([0-9]+)\.html$ [b]/folder[/b]/more.php?oid=$1 [L]

for the second RewriteRule instead, to rewrite yourdomain.com/folder/more123.html to yourdomain/folder/more.php?oid=123 but I'm not really sure if that is what you want to do.

Either way, the syntax is correct, it just depends on where the more.php script is located.

Jim

the_nerd

9:57 am on Oct 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry if I abuse this thread for my own question.

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.

Birdman

1:38 pm on Oct 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

You need to escape any periods because they are reserved for the regex:

RewriteRule ^somepage\.htm$ /someotherpage.php5

I also remove the leading slash, as it's not needed. Also make sure your server can handle the php5 extension.

Regards,
Birdman

jdMorgan

2:23 pm on Oct 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> 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?

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

the_nerd

7:49 am on Oct 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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!

zipadyduda

6:51 pm on Oct 24, 2004 (gmt 0)

10+ Year Member



Jim you are a god among men.

I hope I'm not abusing the thread by posting a very special thanks to Jim and the others for all your help.

With your help I have achieved and exceeded my goals.

Hail webmasterworld