Forum Moderators: phranque

Message Too Old, No Replies

Modrewrite newbie

first time ever!

         

Dilly

12:12 am on Sep 15, 2007 (gmt 0)

10+ Year Member



Hi first time ever mod rewrite attempt on my url on localhost.

heres my htaccess

# Enable mod_rewrite, start rewrite engine
RewriteEngine on
#
# Internally rewrite search engine friendly static URL to dynamic filepath and query
RewriteRule ^Make/([^/]+)/([^/]+)/?$ /Make.php?models=$1&Id=$2 [L]

btw was trying to follow Jims post here [webmasterworld.com]

It comes up with a 404 error any idea why? All help greatly appreciated.

jdMorgan

3:20 am on Sep 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try a simple test first:

# Enable mod_rewrite, start rewrite engine
RewriteEngine on
#
# Redirect "foo.html" to google.com
RewriteRule ^foo\.html$ http://www.google.com/ [R=301,L]

Request "foo.html" from your server, and you should land on Google.

If that works, you'll know that mod_rewrite is enabled and working on your server, and the problem is likely that the regular-expressions pattern does not match your 'friendly' URL correctly.

Jim

Dilly

4:11 am on Sep 15, 2007 (gmt 0)

10+ Year Member



Thanx for the reply jim that test seems to work fine.

So what do you think is wrong with my expression as I dont have a clue and was trying to modify your code from the post with no joy.

[edited by: Dilly at 4:11 am (utc) on Sep. 15, 2007]

Dilly

10:45 am on Sep 15, 2007 (gmt 0)

10+ Year Member



Still cant seem to figure out what is wrong with this damn thing.

Just thought I'd let you know I got this in my httpd.conf file.

<Directory "C:/phpdev/www/">
Options Indexes FollowSymLinks MultiViews Includes ExecCGI
AllowOverride FileInfo
Order allow,deny
Allow from all
</Directory>

Maybe something is wrong here which I dont know about.

[edited by: Dilly at 10:46 am (utc) on Sep. 15, 2007]

jdMorgan

2:26 pm on Sep 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What version of Apache are you running?
What is the URL-path you are requesting to test the rule?
Are you using MultiViews? If not, turn them off.
Exactly how are you determining that "it doesn't work"?

Jim

Dilly

4:35 pm on Sep 15, 2007 (gmt 0)

10+ Year Member



I have no idea what multiviews is so no idea if im using it.

I've determined that it doesnt work by entering the following in the address bar...

[localhost...]

or even without the slash at the end. The $1 and $2 need to be replaced by relevant models and id number. e.g.

[localhost...]

this shoud lead me to...

[localhost...]

But what it is doing is giving me a 404.

I think my apache is set up correctly because the foo.html test worked. Im on apache version 1.3.2.7. Do i need an upgrade? Please say no lol

[edited by: Dilly at 4:38 pm (utc) on Sep. 15, 2007]

jdMorgan

5:22 pm on Sep 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code should work if located in /filename/filename/.htaccess

If it's not located there, then adjust the RewriteRule pattern so it recognizes the correct local URL-path.

For example, if this .htaccess file is located in the site root (home page folder), and /filename/filename/Make is located below that folder, then you'll need


RewriteRule ^filename/filename/Make/([^/]+)/([^/]+)/?$ /Make.php?models=$1&Id=$2 [L]

Remember that .htaccess is a per-directory context; The pattern in the RewriteRule must be adjusted to match the URL-path relative to the current directory. Otherwise, the pattern won't match, and the rule won't run.

If you don't know what MultiViews are, then you're not using them, since it takes a bit of work to configure them. I'd recommend taking that Option out of your Options list for reduced complexity and improved performance. I'd also recommend caution with ExecCGI; Consider enabling it only in your cgi-bin directory, and lock down the file permissions first.

Jim

Dilly

6:24 pm on Sep 15, 2007 (gmt 0)

10+ Year Member



My .htaccess is in the same diectory as the make.php and it still doesn't work.

I'm gonna try the code that you provided with the full path see if that helps.

Thanx for your time jim much appreciated.

Dilly

6:29 pm on Sep 15, 2007 (gmt 0)

10+ Year Member



Just tried the full path as you mentioned above jim it still doesn't work. What Evil is this?

Any other ideas why its doing this?

Dilly

8:05 pm on Sep 15, 2007 (gmt 0)

10+ Year Member



Just tried this and this doesnt work either?

RewriteRule ^Make/1111/5/?$ /Make.php?models=1111&Id=5 [L]

This should work right?

jdMorgan

8:52 pm on Sep 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Were is this .htaccess located?
What is the setting of DocumentRoot?

Jim

Dilly

10:13 pm on Sep 15, 2007 (gmt 0)

10+ Year Member



The htaccess is located here:

C:/phpdev/www/filenameOne/filenameTwo/.htaccess

The Make.php file is located here same as .htaccess:

C:/phpdev/www/filenameOne/filenameTwo/Make.php

DocumentRoot "C:/phpdev/www/"

jdMorgan

11:25 pm on Sep 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



With that setup, you should probably be using:

RewriteRule ^Make/1111/5/?$ /filenameOne/filenameTwo/Make.php?models=1111&Id=5 [L]

and be requesting
example.com/filenameOne/filenameTwo/Make/1111/5/ with your browser.

It's like real estate values: Everything (the code behaviour) depends on location, location, and location. :)

If you moved this code to document root, then it would have to be:


RewriteRule ^filenameOne/filenameTwo/Make/1111/5/?$ /filenameOne/filenameTwo/Make.php?models=1111&Id=5 [L]

And since it's come up twice today already, be sure to completely flush your browser cache before testing any change to your server config code.

Jim

Dilly

11:41 pm on Sep 15, 2007 (gmt 0)

10+ Year Member



Right Jim after some trial an error I have figured out that its the / forward slash that messes things up but have no idea why?

This works..

RewriteRule ^make-([^/]+)-([^/]+)\.php$ Make.php?models=$1&Id=$2 [L]

because it doesnt have any forward slashes.

Also this didnt work due to the capital letter

RewriteRule ^Make-([^/]+)-([^/]+)\.php$ Make.php?models=$1&Id=$2 [L]

I want to add a filename/ before make.php so any idea why Apache doesn't let me use forward slash?

[edited by: Dilly at 11:43 pm (utc) on Sep. 15, 2007]

jdMorgan

11:43 pm on Sep 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're on Apache 2.x, make sure that AcceptPathInfo is off.

Jim

Dilly

11:56 pm on Sep 15, 2007 (gmt 0)

10+ Year Member



im not on Apache 2.x more like 1.3 and theres no acceptpathinfo in httpd.conf

Dilly

12:15 am on Sep 16, 2007 (gmt 0)

10+ Year Member



I've got it working Jim. It was my bad, for some reason I was puting make-1111-5.php as Make-1111-5.php with a capital letter in the address bar and that was screwing things and calling a 404.

Now everything works thanx a lot for your time Jim your expertise and help is very much appreciated.

They need another moderator to help you out in these forums because I think the workload is quite a lot for one person.

Probably will be hard to find someone as good as you ;)

[edited by: Dilly at 12:16 am (utc) on Sep. 16, 2007]

Dilly

12:09 pm on Sep 16, 2007 (gmt 0)

10+ Year Member



Quick question about the rewrite condition Jim.

Have i done this right?

#
# Externally redirect client requests for old dynamic URLs to equivalent new static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{1,9}\ /make\.php\?models=([^&]+)&Id=([^&]+)\ HTTP/
RewriteRule ^make\.php$ http://example.com/make-%1-%2.php? [R=301,L]

[edited by: Dilly at 12:14 pm (utc) on Sep. 16, 2007]

Dilly

1:45 pm on Sep 16, 2007 (gmt 0)

10+ Year Member



I tried this as a test run and it works

# externally redirect direct browser requests for /test.html to /test
rewritecond %{THE_REQUEST} test\.html
RewriteRule ^test\.html$ [localhost...] [R=301,L]
#
# Internally rewrite requests for /test to /test.html
RewriteRule ^test$ /test.html [L]

But when I try to implement the above on here it doesn't work.

# Externally redirect client requests for old dynamic URLs to equivalent new static URLs
RewriteCond %{THE_REQUEST} make\.php\?models=([^/]+)&Id=([^/]+)\ HTTP/
RewriteRule ^make\.php\?models=([^/]+)&Id=([^/]+)$ [localhost.com...] [R=301,L]

Note: my internal rewrite for this is working now so didn't bother putting it up.

I then tried this

# Externally redirect client requests for old dynamic URLs to equivalent new static URLs
RewriteCond %{THE_REQUEST} make\.php\?models=([^/]+)&Id=([^/]+)\ HTTP/
RewriteRule ^make\.php$ [localhost.com...] [R=301,L]

Still no joy, it goes into some kind of a loop.

I think this is correct

RewriteCond %{THE_REQUEST} make\.php\?models=([^/]+)&Id=([^/]+)\ HTTP/

But the beginning of this is not

RewriteRule ^make\.php$ [localhost.com...] [R=301,L]

Thanx again.

[edited by: Dilly at 1:47 pm (utc) on Sep. 16, 2007]

jdMorgan

2:08 pm on Sep 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That looks fine.

What we expect in THE_REQUEST is the entire HTTP request header received from the client. As a loose example:

GET /make.php?models=111&Id=55 HTTP/1.1

So the quantifier I use on the subpattern for the HTTP Method is {3,9} because the shortest valid HTTP/1.1 method is 3 characters long, and the longest is 9. One could just as well use the "+" quantifier, I suppose.

Also, be aware that the query string will have to be exactly what matches the pattern; those two varnames and their values, in that order, and nothing more. Other cases --if they're possible-- would need to be handled in separate [OR]ed RewriteConds.

It wasn't clear what you meant by "it still doesn't work" above -- As a matter of fact, "it doesn't work" --although a common response posted here-- is essentially meaningless without additional details. It could "not work" resulting in a 500-Server Error, or it could "not work" because page-relative links on the pages being produced can't be properly resolved by the client, causing broken images, external CSS, and external JS links. Or it could be anything in between...

More on that: It is the client (browser or 'bot) that resolves page-relative links on pages. It does so using the current 'page' URL shown in the address bar. So, if you're rewriting those 'pages' on the server, the browser doesn't know that, and will request page-linked objects using the page address it knows about -- the one in the address bar. The two solutions are to either rewrite the object requests in addition to the page requests themselves, or to use server-relative or canonical URLs in the on-page object references. i.e. use <img src="/images/logo.gif> or <img src="http://example.com/images/logo.gif"> rather than <img src="images/logo.gif">

I do get quite a bit of help here in this forum, but like me, the other contributors have only a limited amount of time they can spend answering questions. So, having now gone through the wringer and fixed the problem yourself, you're certainly welcome to help out around here as you can... :) I'll confess that one can develop an awful lot of experience answering posts that one does not know the entire answer to. If a successful conclusion is reached, then both the asker and the answerer have learned something. In fact, I've learned much more by answering questions than by asking them.

Jim

g1smd

2:43 pm on Sep 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I help out where I can, but many of the questions are over my head...

Dilly

4:29 pm on Sep 16, 2007 (gmt 0)

10+ Year Member



This looks ok but doesnt redirect to the new friendly url.

RewriteCond %{THE_REQUEST} make\.php\?models=([^/]+)&Id=([^/]+)\ HTTP/
RewriteRule ^make\.php$ [localhost.com...] [R=301,L]

How i tested the above code was by entering the full ugly url path with relevant vars in the address bar such as..

[localhost.com...]

This returned

[localhost.com...]

and said page cannot be displayed

why is it doing this?

g1smd, yeh i have seen you around these forums helping out quite a bit. I am trying to chip in aswell now even though its all very new to me.

[edited by: Dilly at 4:32 pm (utc) on Sep. 16, 2007]

jdMorgan

4:34 pm on Sep 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to explicitly clear the originally-requested query string:

RewriteCond %{THE_REQUEST} make\.php\?models=([^/]+)&Id=([^/]+)\ HTTP/
RewriteRule ^make\.php$ http://localhost.com/filename/filename/make-%1-%2.[b]php? [R[/b]=301,L]

Jim

Dilly

5:08 pm on Sep 16, 2007 (gmt 0)

10+ Year Member



This returns a page cannot be displayed.

RewriteCond %{THE_REQUEST} make\.php\?models=([^/]+)&Id=([^/]+)\ HTTP/ 
RewriteRule ^make\.php$ http://localhost.com/filename/filename/make-%1-%2.php? [R=301,L]

[edited by: Dilly at 5:08 pm (utc) on Sep. 16, 2007]

jdMorgan

7:37 pm on Sep 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Pattern is wrong and substitution URL, too, I think:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /make\.php\?models=([^&]+)&Id=([^\ ]+)\ HTTP/
RewriteRule ^make\.php$ http://localhost.com/make-%1-%2.php? [R=301,L]

Check your error log for help, flush your cache before testing.

Jim

[edited by: jdMorgan at 7:38 pm (utc) on Sep. 16, 2007]

Dilly

9:54 pm on Sep 16, 2007 (gmt 0)

10+ Year Member



Thanx it's working, your the best Jim.