Forum Moderators: phranque

Message Too Old, No Replies

Question: URL Manipulation

         

Munzo

2:09 pm on Apr 13, 2009 (gmt 0)

10+ Year Member



Hello,

I'm looking at getting variables which use dots within them, such as; "example.com/path.example/domain.com/user.range"

How would I go about building a htaccess file using mod_rewrite which will help me interpret this. Even if this is only one large string that is fine but I'd like it to after reading this to go to: "example.com/find.php?=path.example/domain.com/user.range".

Right now my server thinks this is a file and wont even try loading, I don't always know though what will be in url.. as to how many dots at least.

Thanks
Brandon

redhatlab

4:26 pm on Apr 13, 2009 (gmt 0)

10+ Year Member



Hi Munzo,

Have you try the option saying that the URL is not a directory or a file on your .htaccess file.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [R]

g1smd

5:05 pm on Apr 13, 2009 (gmt 0)

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



That code is for a redirect.

It returns a 302 redirect because you used [R] without a number. Redirects should be 301, and the redirect code should also contain the target domain name. You probably do not need a redirect here, though.

What you likely need here is a rewrite. In that case, change one thing in your original code... change [R] to just [L] instead.

Next up, you need to define exactly the format for URLs used on the web, and the format of the parameters used with the file on the server. Once that is done, the actual coding part becomes trivial.

There's a bunch of stuff in a recent thread that might be useful... [webmasterworld.com...]

Munzo

7:17 pm on Apr 15, 2009 (gmt 0)

10+ Year Member



Thanks for the quick replys, although my problem is that it's so trivial I'm but lost to the code which I should use.

See I don't acually know the url that may be sent so I need it to be able to accept anything after the example.com/ and then place it in index.php?find= without trying to see if its a file or directory.

Is that even possible?

g1smd

7:37 pm on Apr 15, 2009 (gmt 0)

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



Yes it is. Capture the information in a backreference, and re-use it in the target.

In this case you need a rewrite, not a redirect, so omit the domain name and the [R] so that the target is a file inside the server, not a new URL out on the web.

That's the difference between a rewrite and a redirect.

Munzo

9:26 pm on Apr 15, 2009 (gmt 0)

10+ Year Member



Perfect stuff, I think you both for your help!

Yay!

jdMorgan

9:26 pm on Apr 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You really do need to check for file-exists -- at least. Otherwise, you won't be able to support a robots.txt or sitemap.xml file, or any images or other 'real' files on this server.

So redhatlab's code with the changes suggested by g1smd is likely very close to what you want to use.

Jim

jdMorgan

9:53 pm on Apr 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd also suggest excluding the index.php file itself, to avoid an unnecessary double-check of the disk after the URL has already been rewritten:

RewriteCond $1 !^index\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]

Jim

Munzo

7:31 pm on Apr 16, 2009 (gmt 0)

10+ Year Member



Alright, wow! thanks.. :P

You say I should allow it to check for certain files or am I able to allow it to see if the file there and if not start to do the writing?

jdMorgan

7:45 pm on Apr 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# If request has not already been rewritten to /index.php
RewriteCond $1 !^index\.php$
# and if request does not resolve to an existing file
RewriteCond %{REQUEST_FILENAME} !-f
# and if request does not resolve to an existing directory
RewriteCond %{REQUEST_FILENAME} !-d
# then rewrite the request to /index.php
RewriteRule ^(.*)$ /index.php [L]

Please don't use this code until you have taken it apart character-by-character using the Apache mod_rewrite documentation [httpd.apache.org], and understand how it works and what it does. Only then can you be sure that it is the correct solution for your site. Using code you find posted on forums without understanding it is a recipe for disaster.

Jim