Forum Moderators: phranque

Message Too Old, No Replies

Can't get mod rewrite to work

nothing happening

         

coho75

2:49 pm on Aug 9, 2004 (gmt 0)

10+ Year Member



Hi,

I am having some trouble getting mod rewrite to work on my site. It doesn't seem to matter what directory I put the htaccess containing the mod rewrite rules in, it never rewrites the URL.

I am using the following:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/cell_phone/(.*)/(.*)/$ /cell_phone.html?brand=$1&model=$2

Any help will be greatly appreciated.

Thanks,
coho75

jdMorgan

7:01 pm on Aug 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteRule patterns in .htaccess should not start with a slash, as this is stripped in an .htaccess context.
Try using "^cell_phone..." instead of ^/cell_phone..."

Jim

coho75

7:11 pm on Aug 9, 2004 (gmt 0)

10+ Year Member



Thanks for your input Jim. However, that didn't seem to fix it. I think I have tried just about everything. I keep getting the original URL returned. I thought it might be an error in my code that was causing it not to work. I checked the server and the module is installed.

coho75

jdMorgan

8:45 pm on Aug 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm... I need to make a list of all the things that can 'break' mod_rewrite, I guess.

Check your AllowOverride directive in httpd.conf -- AllowOverride FileInfo Options is required at minimum.

Is the subdirectory /cell_phone located in the same directory as the .htaccess file with the mod_rewrite code you posted? If not, it may just be a path problem.

You can speed your code up a bit by using


RewriteRule ^cell_phone/([^/]+)/([^/]+)/$ /cell_phone.html?brand=$1&model=$2

but what you have now should at least work.

Jim

coho75

9:05 pm on Aug 9, 2004 (gmt 0)

10+ Year Member



Jim,

I have placed the file in all the related directories without any luck. I have been working on this for about 3 days now. I just can't seem to figure out what is wrong. I have been reading everything that I can find.

I do not actually own the server or have access to it so I cannot change the settings. Anwyay, your help is greatly appreciated.

coho75

coho75

9:09 pm on Aug 9, 2004 (gmt 0)

10+ Year Member



My second variable in the rewrite rule contains a numeric value. Could this possibly be causing the problems? If so, what should I change in my code?

coho75

coho75

1:28 am on Aug 10, 2004 (gmt 0)

10+ Year Member



Any ideas on how I could make this code work for a subdomain. Is there anything different that I would need to add?

coho75

jdMorgan

2:22 am on Aug 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> My second variable in the rewrite rule contains a numeric value. Could this possibly be causing the problems?
The code will accept any characters as the second variable except for "/", which you specified as the delimiter.

> Any ideas on how I could make this code work for a subdomain. Is there anything different that I would need to add?

The code as shown will work in any subdomain. I suspect there is more to your question, though. There are a lot of very different server configurations, and a lot of ways to map subdomains to your server filespace. So please be very specific.

Have you tried a really simple RewriteRule yet, or do you have any working rewriterules? A simple rewrite of a non-existent page URL to an existing, simple html page is a good test:


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^silly\.html$ /existing_page.html [L]

Now request silly.html with your browser, and your server should give you the contents of existing_page.html.

If that doesn't work, it may be time to shop for a new host.

Jim

coho75

3:23 am on Aug 10, 2004 (gmt 0)

10+ Year Member



Jim,

I tried the really simple rewrite rule, and it worked fine. I just can't get the more complex rule working. Off to try and make something work. Thanks for the help.

coho75

coho75

1:12 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



So please be very specific

Ok, here goes. I tried the simple code that was provided in the message above this one. It does exactly what it is supposed to do. However, when I try and make it work in my subdomain it won't.

The url I want to rewrite is in the following format:
ht*p://sub.domain.com/phone_store/cell_phone.html?brand=some&model=thing

When the URL is rewritten I would like it to appear as:
ht*p://sub.domain/phone_store/cell_phone/$1/$2

I have tried many different iterations of the code posted here and have been unable to get any of them to work. I don't receive error messages or anything. The URL just remains the same. Hopefully this description will help me get a more definitive answer. Once again, thanks for all of the help.

coho75

jdMorgan

1:51 pm on Aug 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, this clarifies things. What you are trying to do may be "backwards."

I assume that you are trying to get "search engine friendly" URLs for listing in the search results, and then convert those to calls to a script with query string parameters.

If that is the case, here's how to proceed:

Change your script to produce (output) search engine friendly URLs in the form:
ht*p://sub.domain/phone_store/cell_phone/brand_name/model_number
These URLs will then appear on your pages as links to be followed, and that's what users and search engines will use.

Once a request for a friendly URL arrives at your server, mod_rewrite will convert it into the form:
ht*p://sub.domain.com/phone_store/cell_phone.html?brand=brand_name&model=model_number
and then call your script.

If your script is not actually named "cell_phone.html", then either you'll use a second rewrite, or build that renaming function into the same rule that does the friendly-to-query rewrite. If you *are* calling an html page which then includes php or other scripting, then disregard this, and use ".html" in the substitution URL shown below.

In mod_rewrite parlance, you are rewriting from a search engine friendly URL to an internal file-path with a query string:


RewriteRule ^phone_store/([^/]+)/([^/]+)/?$ /cell_phone.php?brand=$1&model=$2 [L]

The above rule must be placed into the directory pointed-to by the friendly URL; If you map sub.domain.com/ to filespace at domain.com/sub/, then that's where the code should be placed. It also differs from previous posted code in that the trailing slash is now optional, as indicated by "/?$" in the pattern. In your first examples above, you showed the trailing slash, but left it out in subsequent discussion.

Bear in mind that mod_rewrite will only match URLs that exactly match your pattern. There's no latitude there, every hat, dollar, dot, and slash must be correct; If the pattern is wrong for the URL, the rule won't run. Because the simple rule worked fine, I have no doubt that you're being frustrated by a very small error in the code, likely caused by missing a detail in defining your requirements.

The substitution URL-path must also be correct relative to the context in which the rule is invoked; If that path is wrong, then you'll rewrite to a non-existent file -- or worse, to an unexpected file that *does* exist (which can cause real confusion). It's actually better to rewrite to a non-existent file, because at least that way you get something in your error log, instead of generating a page with unexpected contents!

Jim

djgreg

2:16 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



puh, after days and nights of researching for this issue, thankfully I found this thread ;-)

I also want to rewrite a file on a subdomain, but in my case I want to generate a filename like cellphone_12_45.html instead of /cellphone/12/45.html

I have tried to rewrite the solution posted by jd but it failed.

jd could you be so kind and take a look at my .htaccess:


RewriteEngine On
RewriteRule ^index_bilder_(.*).html$ /index_bilder.php?c=$1 [L]

I really don't know what's wrong with it. mod_rewrite works fine in other cases but not with this one.

kind regards
greg

[edited by: jdMorgan at 1:28 pm (utc) on Aug. 11, 2004]
[edit reason] Fixed formatting problem [/edit]

coho75

2:22 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



Jim,

Thanks for your time and input. I finally got it to work. As you stated, my process was a little backwards. Everything looks great now. Thanks again.

coho75

djgreg

5:59 am on Aug 11, 2004 (gmt 0)

10+ Year Member



nobody?
please take a look at my .htaccess!

thanks a lot

greg

jdMorgan

1:33 pm on Aug 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



djgreg,

> I really don't know what's wrong with it. mod_rewrite works fine in other cases but not with this one.

I don't know either. Your code is syntactically correct, and will work fine -- if it is the right code for the problem you are trying to solve.

Without further details, all I can do is suggest that you review all the notes I posted previously in this thread concerning regular expressions, correct location of the code, and proper patterns and pathnames.

Jim

djgreg

2:07 pm on Aug 11, 2004 (gmt 0)

10+ Year Member



ok, basically I am trying to
rewrite index_bilder_FUE.html to index_biler.php?c=FUE
so the user should request the html and the server should take the php.
That's all I want to solve with this code.
After researching a bit more I added the folowing code to the httpd.conf file in the section of the virtual host, on which I want to display the file.
the virtual host is "bilder.urlaubsreise.info"


RewriteCond %{REQUEST_FILENAME} .*/index_bilder_([A-Z]+).html
RewriteRule (.*) /index_bilder.php?c=$1

but this is not working either.
I am really confused. I also posted in the forum of my hosting company (westhost btw ;-) )

greg

jdMorgan

2:37 pm on Aug 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



greg,

Your original code was fine. The only changes I'd make would be to escape the period in front of ".html" and use a forward-looking match on the parameter:


Options +FollowSymLinks
RewriteEngine On
RewriteRule ^index_bilder_([^.]+)\.html$ /index_bilder.php?c=$1 [L]

Other than that, you've got to make sure the files are where they are expected to be, that you've got php loaded, enabled, and working properly as a handler, etc. And if you've done those things, please tell us, so we don't waste mental effort trying to figure out which of a hundred things it could be, and trying to type them all up.

Also, you can break this down into pieces: Try the rewrite one step at a time.

Try this first:


RewriteRule ^index_bilder_([^.]+)\.html$ /index_bilder.[b]html[/b]?c=$1 [[b]R=301[/b],L]

You won't want to use a 301 redirect in the final version, but it's useful as a test in this case; You should see the URL in your address bar change to the query version when you access the original page. If that doesn't work, then there's a problem with invoking mod_rewrite or a problem with the pattern in the rule.

If it does work, then the problem is that the rewrite is being invoked, but that it's rewriting to some unexpected or non-existent path, or maybe it's a problem with invoking php. In those cases, you error log file should provide a good hint as to what's wrong.

That's the best I can do for now without much more specific details, and an indication of what already works, what you have and haven't tried, and what, if anything, is in your server access and error log files.

Jim

djgreg

6:33 am on Aug 12, 2004 (gmt 0)

10+ Year Member



jd, I tried some other things now.

At first I tried your code in the httpd.conf file, then in the local .htaccess.
No success
Then I tried the 301 in both files, no success.
I don't think that it is a problem with a server configuration because I had another mod_rewrite working on this server, but deleted it because I rearanged the site and didn't need it anymore.

For all the options I tried I get a 404 Page not found.
I also tried to open www.domain.com/content/bilder/, because that is where the subdomain "bilder" points to but without success.

I could post you the complete httpd.conf file if you think the error could be in there.

greg

jdMorgan

2:07 am on Aug 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We've had some activity in other threads that may bear on your problem. Specifically, if your LoadModule list is set up incorrectly, then php will kick in before mod_rewrite gets a chance to run. Make sure that php is included in the LoadModule list *before* mod_rewrite -- The modules are invoked in the opposite order from the load list.

I don't know if this will help in your case, but it's worth a shot.

Jim

djgreg

6:01 am on Aug 13, 2004 (gmt 0)

10+ Year Member



Hi,

after numerous tries I got a code that works in the directory.


RewriteEngine On

RewriteCond %{REQUEST_FILENAME} index_bilder_([a-zA-Z]+).html
RewriteRule (.*) index_bilder.php?c=%1

I tried to add this code to the httpd.conf file in the section of the virtual host it should rewrite but without success. Is there anything that needs to be changed when using this code in a httpd.conf or should the code used in .htaccess - files also work in the httpd.conf?

Jim: I have looked after the problem you described but that is not the problem.

thanks a lot for your help

greg

jdMorgan

1:25 pm on Aug 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Is there anything that needs to be changed when using this code in a httpd.conf or should the code used in .htaccess?

In .htacess, the URI "seen" by RewriteRule is stripped of its leading slash. In httpd.conf, the URI's leading slash is not stripped, so your code may be passing this slash into the query string.

So, if your code works in .htaccess and you want to use it in httpd.conf, you'd change the rule to:


RewriteRule [b]/[/b](.*) index_bilder.php?c=%1

Jim

djgreg

3:53 pm on Aug 13, 2004 (gmt 0)

10+ Year Member



aha, okay.
I changed this but still a 404.
When requesting
www.example.info/content/bilder/index_bilder_FUE.html
the code works perfectly, but it does not work when requesting the subdomain which points to this dir:
bilder.example.info/index_bilder_FUE.html

There can't be such a difference in the code can there?
I thougth the mod_rewrite would rewrite no matter what in front of the fielname is? (because of (.*) )

greg

[edited by: jdMorgan at 4:24 pm (utc) on Aug. 13, 2004]
[edit reason] Obscured specifics per TOS [/edit]

jdMorgan

1:51 pm on Aug 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> it does not work when requesting the subdomain which points to this dir:
bilder.example.info/index_bilder_FUE.html

How, exactly, does that subdomain get "pointed" to the subdirectory? The mechanism used to do this may affect your rewriterules and other directives:

There are at least two possible problems:

The new code is not being executed when the subdomain is accessed, because the code is not in the "path" to that subdirectory, or the rewrite to the subdorectory takes place first and bypasses the new code.

The new code *is* being executed, but it is placing the subdirectory name into the "c=" variable along with the requested filename.

Jim