PDA

Click to See Complete Forum and Search --> : Apache Configuration Help


tZ
06-16-2009, 05:57 PM
This is a shot in the dark, but sure some of you have dealt with this same problem.

Would it be possible to create a rewrite rule that would remove index.php from the typical /index.php/controller PHP MVC app route?

What I have: /index.php/blogs/1/20/

What I would like: /blogs/1/20/

So I would ideally like all my paths to look like:

www.whatever.com/blogs/1/20/
www.whatever.com/user/3/
www.whatever.com/blog-entry/1/3/10/

I think you get the idea.

The application is responsible for breaking up all the paths so all I need is a way to route everything without using site.com/index.php/x/y/z/ , but rather site.com/x/y/z/.

any help appreciated as I'm not very familiar with apache configuration.

hewligan
06-16-2009, 10:18 PM
Possible? Yes, but probably not a good idea. Or, at least, a very complicated to pull off one.

Apache's mod rewrite rules are regular expressions. So you'll need to produce a regular expression - or set of regular expressions - that will rewrite the URL to go to the right page (which is easy) but not rewrite the URLs for your images/CSS/Javascript/anything else that isn't a PHP page (not so easy). Including the index.php in the URL makes it easier to identify the URLs for rewriting in your regex. But, as long as there's some systematic way to identify the URLs to rewrite, it can be done.

Drazan
06-16-2009, 10:26 PM
tZ look up .htaccess SEFs and .htacess rewrite and if you are using a blog application put that in there too.

.htaccess is a public text file that overwrites the apache config file without actually doing anything to apache core. Great method so that you won't have to worry about what you did when upgrading.

The htaccess rewrite command will get you where you need to be. This is from a quick google that looks ok.

http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html

Jade

Drazan
06-16-2009, 10:29 PM
Here's the core link.
http://httpd.apache.org/docs/trunk/howto/htaccess.html

http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

tZ
06-16-2009, 10:46 PM
I'm not positive, but I'm thinking something along these lines would work. The only directories at the root of the site are include,vendor and img. So if the beginning doesn't matches any of those rewrite perhaps?


<IfModule mod_rewrite.c>

RewriteEngine on

RewriteRule ^.*?(?<!index\.php|include|img|vendor)/(.*?)$ /index.php/$2 [L]

</IfModule>

hewligan
06-16-2009, 11:07 PM
I'm not positive, but I'm thinking something along these lines would work. The only directories at the root of the site are include,vendor and img. So if the beginning doesn't matches any of those rewrite perhaps?


<IfModule mod_rewrite.c>

RewriteEngine on

RewriteRule ^.*?(?<!index\.php|include|img/vendor)/(.*?)$ /index.php/$2 [L]

</IfModule>


Sounds right to me.

Drazan
06-16-2009, 11:19 PM
oh it has been a while ... you are making my brain work tonight.

Try this if that doesn't work as expected.
This should remove the index.php from the url entirely. Place the .htaccess file in the same directory as the index.php

If it drops your site, then remove the .htaccess file.

And to test that you can actually see the .htaccess file with your ftp. put a blank file in there first.

<IfModule mod_rewrite.c>

RewriteEngine on

RewriteRule ^/([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php/$2/ [R]
RewriteRule ^/([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php/$2 [QSA,L]

</IfModule>

tZ
06-16-2009, 11:37 PM
That results in a apache request error (mine not drazan's - at least yet).

tZ
06-16-2009, 11:40 PM
no go drazan.

404 - not found

I haven't used apache to rewrite anything in a couple months so I'm a little rusty myself.

The site isn't a blog, but has a blog module so to speak. Those URLs were just examples though.

I just need everything to go to the index page unless the beginning of the string is img, vendor, include or index.php.

flutterby nut
06-16-2009, 11:53 PM
i'm a novice to regular expressions, having just learned how to write one a couple days ago...but i understood that you had the carrot (^) then two forward slashes, and what you wanted to include between the slashes...

so, shouldn't there be an ending slash on that second one?...if not, how come?

tZ
06-17-2009, 03:48 AM
I had add a line to application code to look for the pathway var, but this is working. I much rather reset path_info after the rewrite, but that doesn't seem possible.


<IfModule mod_rewrite.c>

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(index\.php|include|vendor|img)
RewriteRule .? /index.php?pathway=%{REQUEST_URI} [L]

</IfModule>