Pregunta

Final edit: It seems that I've gotten this figured out! Answer below.

Edit two: I think Apache is generally a huge pain in the rear, but I'm not in a position to be switching production webservers from Litespeed (Proprietary apache) to nginx, where these URLs resolve without a problem. But yeah, just as a note -- This stuff works just fine in nginx.

Edit three: The following RewriteRule has been figured out so far from a friendly fellow on freenode who tried to help me out some:

RewriteRule ([^#]+)#([^#]+) /index.php/$1¦$2 [L,QSA,NC]

However cool this crazy moon language regex looks, it only works for URLs with a hash in them -- It busts everything else. Is there a way to make these RewriteRules fall back if they 404 or anything like that?

Edit four: Someone suggested:

FallbackResource /index.php

No luck, either.

The original question:

I'm working on a site which has certain urls that have URLs which are derived from the title of the item (As an example for a title, thisUrlDerivedFromUrl - #example), that look something like this:

http://example.com/listen/thisUrlDerivedFromItemTitle+-+%23example-mid26372

This, however, brings me to the following 404 error page, where you can see that the request gets cut off at the # encoded as %23:

Error 404
Unable to resolve the request "listen/thisUrlDerivedFromItemTitle+-+"

To be clear, I'm -not- looking for fancy-escaped AJAX urls or anything like that which comes up when I try and google for solutions. This is simply an issue of a bunch of certain pre-existing items on this particular site which aren't resolving due to the presence of a # character in the URL, which is a value derived from the title of the item, where people like throwing hashtags that don't go anywhere and that sort of thing or stuff like "i am #1" in the title of their uploaded items.

So, here's the kicker -- In Yii, there's an option to hide the script name (index.php) using the

showScriptName => 'false'

directive in config/main.php. This is the desired behaviour of the site, according to the site's operator. However, I've got showScriptName disabled for now because the URLs with hashes in them aren't working without it, which makes the URLs all look something like this:

http://example.com/index.php/listen/thisUrlDerivedFromItemTitle+-+%23example-mid26372

The thing is that this URL format resolves just fine, no 404 mess or anything like that. I don't think my employer is happy with this compromise.

I'll provide my .htaccess, as well as what my urlManager array looks like in config/main.php, in the hopes that someone might have some answer to the end of finding a solution to this problem. Any ideas, or anything else I could be sharing in order to seek a solution are helpful and appreciated in advance :)

.htaccess:

Options +FollowSymLinks

IndexIgnore */*

RewriteEngine on

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]

urlManager array in config/main.php:

'urlManager' => array(
    'urlFormat' => 'path',
    'showScriptName' => true,
    'rules' => array(
        'user/<_a:(register|update|forgottenpassword)>' => 'user/<_a>',
        'user/<username>' => 'user/view','user/<username>/<_a>' => 'user/<_a>',
        'listen/<title>-mid<id>' => 'mixtape/listen'),
),

EDIT: Apparently these URLs worked fine before I went and muntzed up the htaccess file, which in its previous incarnation, was muntzing up facebook being able to access static image file links for opengraph. I contest this assertion that it worked fine before based on testing it out with the backups - switching it back to the old htaccess&code does zilch. Here it is for reference, however:

Options +FollowSymLinks

IndexIgnore */*

RewriteEngine on


RewriteCond %{HTTP_REFERER} !^http://example.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://example.com$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com$      [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|mp3|zip)$ - [NC,F,L]

RewriteBase /

# if a directory or a file exists, use it directly

RewriteCond %{REQUEST_FILENAME} -s [OR]

RewriteCond %{REQUEST_FILENAME} -l [OR]

RewriteCond %{REQUEST_FILENAME} -d

# otherwise forward it to index.php 

RewriteRule ^.*$ - [NC,L]

RewriteRule ^.* - [F,L]

RewriteRule ^.*$ index.php [NC,L]
¿Fue útil?

Solución

I've got it figured out now thanks to a friendly stranger on freenode hooking it up with the following crazy moon language regex workaround:

RewriteRule ([^#]+)#?([^#]*) /index.php/$1$2 [L,QSA,NC]

Apparently I don't really speak crazy moon language, but this is what I infer it means: it's saying match $1 () is character group of anything not including # ([^#] means not #) up to a #, and then match $2 is the same the other side, with a wildcard * after any incident #'s.

Cheers to anyone else who considered an answer at all!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top