Directories and Mod_Rewrite

New to mod_rewrite? This is a good place to start.

Directories and Mod_Rewrite

Postby Ian » Sun Jul 12, 2009 3:53 pm

Hi there :)

I'm guessing this should be moved to the newbies corner :roll:, as I'm guessing this may have quite a straight forward solution...

I am new to Mod rewrite and am really struggleing to get my head round it, despite all the help I have sourced on this site or the internet as a whole!

I am currently using the below code for a site I am developing:

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule \$ - [L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?parent=$1&content=$2 [L]
RewriteRule ^([^/\.]+)$ index.php?content=$1 [L]

ErrorDocument 404 /404


However, I have stumbled accross two problems:

1)Accessing my site at domain.com is fine, however, accessing my site via ip.ad.dre.ss/~site causes problems. I am guessing this is something to do with my lack of a RewriteCond, using %{DOCUMENT_ROOT} ? I have played around with this but with no success.

2) The Error Document - not mod_rewrite I know, but because, for example, the page domain.com/a/b is showing domain.com/a/404, whereas I want it to show domain.com/404. Could I use a similar method as above (i.e %{DOCUMENT_ROOT}) to rectify this?

I hope I have made myself clear enough, and would really appreciate any help :)

Many thanks
Ian.[/code]
Ian
 
Posts: 6
Joined: Sun Jul 12, 2009 3:41 pm

Postby richardk » Sun Jul 12, 2009 4:12 pm

What are you trying to achieve?

Code: Select all
RewriteRule \$ - [L]

You want to ignore all URLs with a $ in them? Are you trying to not match the variables ($1)?

1)Accessing my site at domain.com is fine, however, accessing my site via ip.ad.dre.ss/~site causes problems.

What problems exactly?

You might need a RewriteBase
Code: Select all
RewriteBase /~site/

but that will probably break accessing via example.com.

You could set up a redirect from the IP address to the domain name.
Code: Select all
ErrorDocument 404 /404
Options +FollowSymLinks

RewriteEngine On

# If it isn't the domain name
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteRule ^([^/.]+)/([^/.]+)$ index.php?parent=$1&content=$2 [L]
RewriteRule ^([^/.]+)$          index.php?content=$1           [L]

Or
Code: Select all
# If it is the IP address
RewriteCond %{HTTP_HOST} ^1\.2\.3\.4$ [NC]


2) The Error Document - not mod_rewrite I know, but because, for example, the page domain.com/a/b is showing domain.com/a/404, whereas I want it to show domain.com/404.

I'm not sure how that can be happening. /a/b should match the second rule and therefore isn't a 404 error.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Ian » Sun Jul 12, 2009 5:24 pm

Hi Rich, thanks very much for your response.

richardk wrote:What are you trying to achieve?
Code: Select all
RewriteRule \$ - [L]

You want to ignore all URLs with a $ in them? Are you trying to not match the variables ($1)?

I'm going to be honest, I wasn't really sure what this was for... seems to be working fine with me having deleted it...

richardk wrote:
1)Accessing my site at domain.com is fine, however, accessing my site via ip.ad.dre.ss/~site causes problems.

What problems exactly?


It's fine with the root (00.00.00.00/~site) but as soon as you try navigating to anywhere else (00.00.00.00/~site/a) it gives you the standard Apache 404 Not Found page - "The server cannot find the requested page: 00.00.00.00/~site/a (port 80). Please forward this to the webmaster blah blah blah"

richardk wrote:You might need a RewriteBase
Code: Select all
RewriteBase /~site/

but that will probably break accessing via example.com.

You could set up a redirect from the IP address to the domain name.
Code: Select all
ErrorDocument 404 /404
Options +FollowSymLinks

RewriteEngine On

# If it isn't the domain name
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteRule ^([^/.]+)/([^/.]+)$ index.php?parent=$1&content=$2 [L]
RewriteRule ^([^/.]+)$          index.php?content=$1           [L]

Or
Code: Select all
# If it is the IP address
RewriteCond %{HTTP_HOST} ^1\.2\.3\.4$ [NC]



Thing is I need to be able to view the page in "IP mode". I'm planning to use it as a developing area before actually forwarding the DNS to my domain. I am doing this process at the moment so as when it comes to forwaring the DNS servers I don't have to change anything in the site - therefore all links and pages have to work in both guises - the domain and the IP address.

richardk wrote:
2) The Error Document - not mod_rewrite I know, but because, for example, the page domain.com/a/b is showing domain.com/a/404, whereas I want it to show domain.com/404.

I'm not sure how that can be happening. /a/b should match the second rule and therefore isn't a 404 error.

It was a bad example and not quite what I meant, but I have decided to use PHP headers for 404 errors now so no need for this.


Would it be possible to set the RewriteBase as the the directory that the .htaccess file is in? That would pretty much solve my problems.

Thanks again for any advice.
Ian
 
Posts: 6
Joined: Sun Jul 12, 2009 3:41 pm

Postby richardk » Wed Jul 15, 2009 12:27 pm

Would it be possible to set the RewriteBase as the the directory that the .htaccess file is in? That would pretty much solve my problems.

You can't use a variable to set a RewriteBase.

Try
Code: Select all
ErrorDocument 404 /404
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI}::$0 ^(/(.+/)?)(.+)::\3$
RewriteRule ^.*$ = [E=BASE_PATH:%3]

RewriteRule ^([^/.]+)/([^/.]+)$ %{ENV:BASE_PATH}index.php?parent=$1&content=$2 [L]
RewriteRule ^([^/.]+)$          %{ENV:BASE_PATH}index.php?content=$1           [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Ian » Wed Jul 15, 2009 4:07 pm

Thanks for this Richard.

However, there was no luck with the code. All pages on the IP version are now showing the Apache 404 Error page, whereas on the domain name version all pages are displaying as a 500 internal server error.

I have played about with the code but (with especially with what you added) I am well out of my depth and my ideas came to nothing.

Any other suggestions would be greatly appreciated.
Ian
 
Posts: 6
Joined: Sun Jul 12, 2009 3:41 pm

Postby richardk » Thu Jul 16, 2009 1:42 pm

Try
Code: Select all
ErrorDocument 404 /404
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI}::$0 ^(/(.+/)?)(.+)::\3$
RewriteRule ^.*$ - [E=BASE_PATH:%1]

RewriteRule ^([^/.]+)/([^/.]+)$ %{ENV:BASE_PATH}index.php?parent=$1&content=$2 [L]
RewriteRule ^([^/.]+)$          %{ENV:BASE_PATH}index.php?content=$1           [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Ian » Fri Jul 17, 2009 10:06 am

richardk wrote:Try
Code: Select all
ErrorDocument 404 /404
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI}::$0 ^(/(.+/)?)(.+)::\3$
RewriteRule ^.*$ - [E=BASE_PATH:%1]

RewriteRule ^([^/.]+)/([^/.]+)$ %{ENV:BASE_PATH}index.php?parent=$1&content=$2 [L]
RewriteRule ^([^/.]+)$          %{ENV:BASE_PATH}index.php?content=$1           [L]


Rich, you are a genius. Thanks so much...

One last question though, seeing as now I am still messing around with my 404 errors....

Would it be possible to add the same principle to the error document? I tried
Code: Select all
ErrorDocument 404 %{ENV:BASE_PATH}/404

but it didn't like it. Even more preferable would be to make the .htaccess page actually forward to the the 404 page using the above method, rathing than just masking the URL, if you see what I mean?

Cheers
Ian
 
Posts: 6
Joined: Sun Jul 12, 2009 3:41 pm

Postby richardk » Fri Jul 17, 2009 10:50 am

You can't use variables in ErrorDocuments. And i don't think relative paths work either.

You could use mod_rewrite
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI}::$0 ^(/(.+/)?)(.+)::\3$
RewriteRule ^.*$ - [E=BASE_PATH:%1]

RewriteRule ^([^/.]+)/([^/.]+)$ %{ENV:BASE_PATH}index.php?parent=$1&content=$2 [L]
RewriteRule ^([^/.]+)$          %{ENV:BASE_PATH}index.php?content=$1           [L]

# If it's not a request to an existing file
RewriteCond %{SCRIPT_FILENAME} !-f
# and it's not a request to an existing directory
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule . %{ENV:BASE_PATH}404 [L]


Even more preferable would be to make the .htaccess page actually forward to the the 404 page using the above method, rathing than just masking the URL, if you see what I mean?

If the URL changes it becomes a redirect not a 404 error.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Ian » Wed Jul 22, 2009 3:18 am

Ah, I see what you are saying. Not a problem - I shall go around it a slightly different way.

One last thing - and sorry to keep on! - is that I am now having problems with my trailing slashes. One trailing slash is OK, using the code
Code: Select all
RewriteRule ^(.+)/$         %{ENV:BASE_PATH}$1       [R=301,L]


However, when two or more trailing slashes are put on the end - say the URI becomes domain.com/contact/email// (or ip.ad.dre.ss/~user/contact/email//) it redirects to domain.com/home/username/public_html/contact/email (or ip.ad.dre.ss/home/username/public_html/contact/email, which brings up an apache 404 because the ~username becomes eradicated).

Again I've tried a few things to make all trailing slashes redirect to the same inputted page without any slashes but to no avail - I assume it's an easy problem to overcome?

Thanks again...
Ian
 
Posts: 6
Joined: Sun Jul 12, 2009 3:41 pm

Postby richardk » Wed Jul 22, 2009 10:51 am

You can use REQUEST_URI instead
Code: Select all
RewriteCond %{REQUEST_URI} ^(/.*[^/])/+$
RewriteRule /$ %1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Next

Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 28 guests

cron