Forum Moderators: phranque

Message Too Old, No Replies

.htaccess (Another one!)

         

Elwon20

12:22 pm on Sep 30, 2011 (gmt 0)

10+ Year Member



Hi Guys and Gals,

I was hoping some bright spark out there could help me with a little .htaccess/regex problem.

I need to rewrite all urls to one php file. For example:

[mydomain.com...]
directs to
[mydomain.com...]

and

www.mydomain.com/members/test/url/
directs to
[mydomain.com...]


Everything i've tried seems to end up in an infinite loop :/ Never was any good at regexp!

I'm sure someone more intelligent than I can come up with the answer off the top of their head.

Thanks in advance!

Elwon20

12:24 pm on Sep 30, 2011 (gmt 0)

10+ Year Member



Perhaps I should have put those urls in code tags?

http://www.mydomain.com/members

directs to
http://www.mydomain.com/index.php/members


and

www.mydomain.com/members/test/url/

directs to
http://www.mydomain.com/index.php/members/test/url

Elwon20

2:22 pm on Sep 30, 2011 (gmt 0)

10+ Year Member



Here is what I have so far:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.php/.*
RewriteRule (.+) /index.php/$1 [L]


However this is also rewriting my css styles. What can I do to prevent it from converting anything with a . in the name?

Elwon20

4:46 pm on Sep 30, 2011 (gmt 0)

10+ Year Member



Just in case anyone was wondering I ended up with this:

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

The first two lines negate the second two lines if the requested file exists (without them my css files get redirected aswell!), if it doesn't exist then it forwards to index.php as described in my other post.

g1smd

5:40 pm on Sep 30, 2011 (gmt 0)

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



The literal period in the RewriteCond pattern needs to be escaped using the \. format.

The trailing
.*
forces the parser to read to the end of the URL request but then does nothing with that data. Delete the
.*
here.

The -f and -d checks are very inefficient. For URLs that will never be rewritten, add another RewriteCond that excludes
\.(png|jpe?g|gif|etc|etc)$
requests. List this condition first. Make sure the "not index.php" RewriteCond is listed next. This ensures that the -f and -d checks do not run for every request made of your server, only for those that will potentially need to be rewritten.

lucy24

7:17 pm on Sep 30, 2011 (gmt 0)

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



If your server will cooperate (it depends on when they add "index.html" or equivalent to requests for directories) you can save a whole lot of time by putting the extension into the Rule itself, like:

RewriteRule {blahblah}\.php$ {et cetera}

Oh, and I think you mean either

/index.php/$1 [R=301,L]
or
index.php/$1 [L]

but if g1 says I am mistaken, listen to him.

g1smd

7:24 pm on Sep 30, 2011 (gmt 0)

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



No, it's supposed to be a rewrite and it uses the AcceptPathInfo feature to pass information to the index.php script, not as parameters but as more stuff in the path.

I avoid this method. It sometimes has disadvantages. AcceptPathInfo is a tricky animal, and can cause Duplicate Content issues.

The other method of passing stuff to a script file uses parameters, but unencoded slashes are not valid in a query string. That's why the AcceptPathInfo method is sometimes used, as here.

The URL request
example.com/this/page/here
is rewritten to
/index.php/this/page/here
where the script at
/index.php
gets to process
/this/page/here
as the path data.

Most people would think about rewriting the request to
/index.php?vars=this/page/here
but unencoded slashes are NOT valid in a query string.