Forum Moderators: phranque

Message Too Old, No Replies

Issue with RewriteRule

Issues with IPBoard and IPContent along with RewriteRule

         

Putzy

10:22 pm on Nov 10, 2009 (gmt 0)

10+ Year Member



Hey guys. Long time reader, first time poster (after a recommendation to do so!)

I'm not sure how familiar you are with IP.Board and IP.Content.. well, when you activate .htaccess redirection in the ACP, you get a .htaccess file generated like so:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Now, with IP.Content - I am using IPC to put content onto the 'Featured' section of my site. It grabs content from a hidden forum and displays it - simple. An example URL for a view page is http:// mydomain.com /featured/view.php?group=fm&article=7822. The forum board itself is within /community.

So, I've tried adding in some SEO to the links to put the title into the URL for these view pages. I can deal with the PHP side of things no problem, that's simple (in fact, I already have that covered), what is getting me is the RewriteRule. I want the URL to look something like this instead: http:// mydomain.com /featured/article/fm/7822-this-is-my-article-title.

I'm not a massive .htaccess guy... so have tried my best to get this working by adding these lines into the .htaccess file mentioned above:

RewriteRule ^/featured/article/(.*)/(.*)-(.*)$ /community/index.php?app=ccs&module=pages&section=pages&folder=featured&page=view.php&group=$1&article=$2&title=$3 [L]
RewriteRule ^/featured/article/(.*)/(.*)-(.*)$ /featured/view.php?group=$1&article=$2&title=$3 [L]

The first line uses the FULL url created by IPB to access the page, the second URL is the 'semi-friendly' one created by the IPC application which can be user-defined. The problem is, both of these attempts result in me getting a neat little 404 Not Found page.

If anyone here can recommend what to do to fix this - it'll be appreciated. If you need any more details - just let me know and I'll try and hook you up with what you need. If you need more contact with me - email address is in profile.

Thanks a lot,

Lewis :)

jdMorgan

11:15 pm on Nov 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your code is in .htaccess, there will never be a leading slash on the URL-path examined by RewriteRule. So, remove the leading slash from the RewriteRule pattern for a start.

Also, you would be wise to avoid the use of the promiscuous and greedy ".*" regex pattern unless absolutely necessary -- and especially multiple occurrences in a single pattern. Your current patterns will result in hundreds, thousands, or even tens of thousands of 'back-off-and-retry' matching attempts for every HTTP request for any "/featured/article/" URL-path.

For your first rule as an example, I'd suggest:


RewriteRule ^featured/article/([^/]+)/([0-9]+)-(.+)$ /community/index.php?app=ccs&module=pages&section=pages&folder=featured&page=view.php&group=$1&article=$2&title=$3 [L]

A pattern such as this can be evaluated in a single left-to-right pass. If this is not clear, please see the references cited in our Forum Charter.

Jim

Putzy

11:34 pm on Nov 10, 2009 (gmt 0)

10+ Year Member



Hi Jim,

Help appreciated there. Will keep everything on board in future. Once again though (like I expected), I get a normal 404 error.

Perhaps I should clarify the system I have working.. might help a bit.

I have two .htaccess files on my site, one direct in /public_html and one in /public_html/community.

The content of the htaccess in the root directory is:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

I believe this removes index.php from my URL, so I can access directly from http:// mydomain.com /featured, rather than http:// mydomain.com /index.php/featured.

The content of the htaccess in the /community directory is:

RewriteEngine On
RewriteBase /community/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\..+$
RewriteRule . /community/index.php [L]

This does a similar thing to the script above, but this time on my forum rather than my mainsite area.

Anyhow - adding the line you supplied (into either file) is causing the 404 problem.. I'm wondering if the RewriteBase or those RewriteCond lines are messing with the remainder of the script - removing them however, either causes all pages on my site to get 404, or causes a Internal Server Error (which is not being documented in my server log)

Once again.. thanks for any help.

Lewis

jdMorgan

12:04 am on Nov 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If "RewriteOptions inherit" is set, the code in /community/.htaccess will never run for any requested URL which does not resolve to a physically-existing file, as should be clear from the /.htaccess code...

None of this code affects your URLs, as both rules above are internal rewrites. URLs are *defined* by the links on your pages, and nothing on the server can "change" them once they appear on your pages. mod_rewrite can either redirect a client-requested URL, or it can change the URL-to-filepath association of a client-requested URL, but it cannot change a URL.

I assume that you added the new rule ahead of the existing one in /.htaccess. If not, that's where it must go.

[added] Look at your server error log for 500-Server Error information, not the server access log.

Jim

Putzy

8:57 pm on Nov 11, 2009 (gmt 0)

10+ Year Member



Hey again Jim,

Well, I've tried adding the line you supplied above (with the RewriteRule) above the "RewriteRule . /index.php [L] " line, and the RewriteCond lines, and I still get the 404 message. So, I tried adding in a simple redirect:


RewriteRule ^mypage.php$ /index.php [L]

and I still get the 404 page. Even adding in your RewriteOptions line gives me no results.

The Internal Server Error messages are not being recorded in the error log at all - so I can't work from them either. Normally (I have received them myself before) they are getting recorded - so I don't think there is a problem that side of things.

This seems to be a very strange problem - and I am quite literally stuck into a corner now. Could it be something as simple as a Apache config problem? The trouble with that is we are currently using shared hosting (of with .htaccess use IS enabled btw), and as far as I can tell, we have no way to configure anything.

Lewis.

jdMorgan

10:47 pm on Nov 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



May we have an example URL-path that you are using to do these tests? Please replace your domain with "example.com" but leave everything else exactly as-typed (unless it violates our TOS for some reason).

Thanks,
Jim

Putzy

10:51 pm on Nov 11, 2009 (gmt 0)

10+ Year Member



No problem. I'll give it as the end-user will see it.

I would like this URL: http://example.com/featured/view.php?article=8321&group=fm&title=fm-club-2010, rewritten to this URL: http://example.com/featured/article/fm/8321-fm-club-2010

Thanks again.

Lewis

jdMorgan

11:11 pm on Nov 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case, publish the link to "http://example.com/featured/article/fm/8321-fm-club-2010" in the html href on your page, and the code posteed above will deliver requests for that link to your script at "/featured/view.php?article=8321&group=fm&title=fm-club-2010"

mod_rewrte does not 'edit' your pages, it simply modifies the mapping of incoming URLs, as requested by HTTP clients, to the filespace on your server. The link you publish on your page is the URL, and there is nothing that anything anywhere can do to change it -- the URL is defined by appearing in a link on a page, even if that URL does not resolve to an existing resource anywhere on the Web.

Jim

Putzy

11:15 pm on Nov 11, 2009 (gmt 0)

10+ Year Member



Yeah - I understand all of that - has actually confirmed a few things I was not sure of before hand.

The issue I have though is with the rewriterule, as when accessing the "http://example.com/featured/article/fm/8321-fm-club-2010" link, I get a 404 not found, because the URL is not remapping to "http://example.com/featured/view.php?article=8321&group=fm&title=fm-club-2010" correctly for some strange reason (which is what I am needing the help with)

I can deal with all the PHP changes to get the URL 301 redirecting if it is incorrect etc after actually getting the rewriterule working correctly.

jdMorgan

11:37 pm on Nov 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please rename or delete the .htaccess files in root and /community, and test the following code in example.com/.htaccess :

RewriteEngine on
#
# Rewrite featured articles to community/index.php script with query string
RewriteRule ^featured/article/([^/]+)/([0-9]+)-(.+)$ /community/index.php?app=ccs&module=pages&section=pages&folder=featured&page=view.php&group=$1&article=$2&title=$3 [L]
#
# Rewrite non-blank /community URL-path requests with no filetype and which
# do not resolve to existing files or directories to /community/index.php
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^community/. /community/index.php [L]
#
# Rewrite all other non-blank URL-path requests to /index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Jim

Putzy

7:15 am on Nov 12, 2009 (gmt 0)

10+ Year Member



Hey again Jim,

Tested that code and the RewriteRule for the community and all the other areas work, but once again, my /featured/article links do not.

Is there a possible way to find out exactly where the requests are trying to route to (ie: the /community/index.php bit is incorrect) that I could code onto my 404 page to try to look at?

Lewis

jdMorgan

12:47 pm on Nov 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The filepath that the server attempted to resolve the requested URL to will be logged in your server error log.

Otherwise, you could use phpinfo() to dump all the server variables onto your 404 page for review.

Jim

Putzy

7:51 pm on Nov 12, 2009 (gmt 0)

10+ Year Member



Right.. managed to get the QUERY_STRING from phpinfo on the 404 page:
app=ccs&module=pages&section=pages&folder=featured&page=view.php&group=fm&article=8321&title=fm-club-2010

So - from what I can tell it looks like it is sending the request to the right page, but it is missing the correct folder in the redirect? I can't help but think it is something to do with the /community RewriteRules?

jdMorgan

10:32 pm on Nov 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the incorrect folder that you refer to, and what is the correct value?

Please remember that we don't know your site at all, and have no way to tell correct from incorrect unless you state things explicitly. From the query string you posted, it appears that some other agent has redirected or rewritten this request, as many of the parameters shown do not appear in the rules discussed above.

Jim