Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2016-06-07 07:48:29

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

htaccess rewrite woes

I have a seemingly simple RewriteRule very similar to one I’ve successfully used elsewhere before to act as a language-specific alias for category/{something} that I just cannot get to work properly. I always get a 404. Can you see anything wrong with it?

RewriteRule ^rubrik/([^/]+)$  index.php?c=$1 [L]

I also have no luck with:

RewriteRule ^rubrik/([^/]+)$  category/$1 [L]

Weirdly, this RewriteRule in the same .htaccess file works fine:

RewriteRule for timestamped js/css
RewriteRule ^assets/(js|css)/([_a-z0-9]+)\.(min\.)?([0-9]+)\.(js|css)$  assets/$1/$2.$3$5 [L]

Things I’ve checked / tried:

  • It’s within the <IfModule mod_rewrite.c>, after RewriteEngine On but before txp’s own rules and there’s no RewriteBase applied.
  • mod_rewrite is definitely loaded and AllowOverride is set to All (a standard MAMP setup).
  • I can’t see another htaccess higher up in the directory tree that might be conflicting with it.
  • I’ve tried switching the order of the RewriteRules and commenting out the other one.
  • The destination url – both index.php?c=category-name and category/category-name – works correctly.
  • I can’t see anything useful in the apache or php log files (but I do see something if I mistype the rule, so it is working).

I’m using 4.6beta/dev and this is a multi-site installation on a local MAMP installation using PHP 5.6.10. The .htaccess file is in the /sites/site-name/public directory and when I make other changes to that file (e.g. commenting out the second rule), it does affect the site, so I am working on the correct file.

Related question: How do you debug this. I can test the regex rules in Patterns or similar and they work as expected, but how do you debug htaccess in the actual server situation?


TXP Builders – finely-crafted code, design and txp

Offline

#2 2016-06-07 08:22:09

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: htaccess rewrite woes

If it helps, I worked out how to add RewriteLog and RewriteLogLevel to the virtual host container in MAMP and get the following logs:

Both go through the motions and seem to rewrite the url before ending up with initial URL equal rewritten URL: /Users/me/Sites/###/sites/###/public/index.php [IGNORING REWRITE]. I’m non-plussed. Any ideas?


TXP Builders – finely-crafted code, design and txp

Offline

#3 2016-06-07 11:26:59

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: htaccess rewrite woes

I’m far from a rewrite expert, but try removing the [L] from the rule. As far as I recall, that tells Apache it’s the [L]ast rule in that chain and it should not process any more rules after it matches that one. What that means is that Txp’s internal category rewriting, which converts /category into the behind-the-scenes index.php/blahblah, won’t run. That in turn will trigger a 404 because the /category folder doesn’t really exist in the filesystem.

The reason it works with the other ‘asset’ rules, btw, is because they are real folders, so once it drops out of the .htaccess file, Apache has all the information it needs to serve the files from the filesystem. In Txp’s case, it needs a further rule to run before it can resolve to index.php properly.

If that doesn’t work, the other way to do it might be to leave the [L] and explicitly specify index.php in the destination link. That should give Apache all it needs to discover the file.

Hope that fixes it.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#4 2016-06-07 20:12:12

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: htaccess rewrite woes

Thanks Stef for your tips but no luck. I restarted everything, server, laptop etc. and went back to a completely naked textpattern .htaccess file and then added just the following line in the position described above:

RewriteRule ^rubrik/([^/]+)$  index.php?c=$1 [L]

Both give a 404. I just can’t see what I’m doing wrong.

Here are the logs:

and a diff shows (aside from timestamp and hash) about 5 differences. Looking at the logs:

split uri=index.php?c=veranstaltungen -> uri=index.php, args=c=veranstaltungen

it seems to recognise and process the regex fine. The version without [L] flag then tries to match index.php/veranstaltungen (??) but the rest of it goes over my head…


TXP Builders – finely-crafted code, design and txp

Offline

#5 2016-06-09 09:29:24

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: htaccess rewrite woes

A small bit of progress: if I create a section called rubrik and do the rewrite as follows:

RewriteRule ^rubrik/([^/]+)$  index.php?s=rubrik&c=$1 [L]

it works fine!

It just means I need to manually handle categories in the section using article_custom category='<txp:category />' section="article-section" … instead of using txp:article’s automatic context detection.


TXP Builders – finely-crafted code, design and txp

Offline

#6 2016-06-09 10:18:38

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: htaccess rewrite woes

So the final solution – without needing a dummy section – seems to be:

RewriteRule ^rubrik/([^/]+)?/?$  index.php?s=default&c=$1 [QSA,L]

This allows one to use urls with the word rubrik instead of category and to list articles by category as usual using txp:article via the default section template.

Changes versus the version(s) above:
  • The addition of s=default seems to be necessary even though the destination url works fine when manually entered without it. Ive no idea why.
  • The addition of ?/? prevents it 404-ing if no category is supplied (first ?) or a trailing slash is used (second ?)
  • The addition of QSA (=Query String Append) to the [L] flag at the end ensures that txp’s pagination variables continue to be processed …

TXP Builders – finely-crafted code, design and txp

Offline

#7 2016-06-09 12:52:11

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

Re: htaccess rewrite woes

Thanks for documenting and explaining, jakob, that’s very helpful!


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

Board footer

Powered by FluxBB