2

i have a very simple rewrite rule, however it is not working.

i have the following:

#old see edits for newer versions
RewriteCond %{HTTP_REFERER}           ^localhost:8080(/|$)
RewriteRule ^/downloads/.*  /somePackage/index.php?id=5  [NC,C]
RewriteRule ^/downloads   /somePackage/index.php?id=2  [NC,L]

it seems that it doesn't pass the RewriteCond, why is this the case?

when i try this:

RewriteRule downloads/?    /somePackage/index.php?id=2 [NC,L]

it seems to work for localhost:8080/downloads

but when i try:

RewriteRule downloads/.*/?    /somePackage/index.php?id=5 [NC,L]

it doesn't work, why does this not work? what should i do instead?

edit:

i this is the current code which only works for localhost:8080/downloads and localhost:8080/downloads/ but not when i add something after the slash

RewriteRule downloads/somedownload/?  /somePackage/index.php?id=5  [NC,L]
RewriteRule downloads/?   /somePackage/index.php?id=2  [NC,L]

edit 2:

here is the .htaccess code using for this.

# Only the URLs /downloads and /downloads/ (with and without trailing slash)
RewriteRule ^downloads/?$ /somePackage/index.php?id=2 [NC,L]

#gives 404 - Category not found
RewriteRule ^downloads/ignis_desktop$ /somePackage/index.php?id=5 [NC,L]

# Everything else that starts /downloads/<something>
RewriteRule ^downloads/. /somePackage/index.php?id=5 [NC,L]

i can't seem to find the solution to the 404.

however with [r=301] it seems to redirect correctly to the page but it breaks the purpose of the rewrite: making the url fancy.

2 Answers2

0

HTTP_REFERER (if set) is an absolute URL starting with a scheme. eg.

http://localhost:8080/....

So, a CondPattern such as ^localhost:8080(/|$) would never match.

Note also that RewriteCond directives only apply to the single RewriteRule that follows, not all of them, as your first code sample seems to be suggesting.

RewriteRule ^/downloads   /somePackage/index.php?id=2  [NC,L]

A RewriteRule pattern such as ^/downloads (ie. the URL path starting with a slash) will never match in per-directory .htaccess files. In .htaccess files the directory-prefix is first removed from the URL-path before pattern matching. So, this would need to be ^downloads to match all URLs that start with /downloads.

RewriteRule downloads/.*/?    /somePackage/index.php?id=5 [NC,L]

This will not match localhost:8080/downloads since the pattern requires a trailing slash.

UPDATE:

I want localhost:8080/downloads to go to id=2 and localhost:8080/downloads/somedownload to go to id=5 and localhost:8080/downloads/anotherdownload to go to id=6 however for the question I have compacted it to /downloads/<everything-else> go to id=5

I assume /downloads is not a physical directory on the filesystem. In which case you should use an end of string anchor ($) on the RewriteRule pattern, or be careful about the order in which you place these directives (ie. most specific first).

Try the following:

# Only the URLs /downloads and /downloads/ (with and without trailing slash)
RewriteRule ^downloads/?$ /somePackage/index.php?id=2 [NC,L]

# Everything else that starts /downloads/<something>
RewriteRule ^downloads/. /somePackage/index.php?id=5 [NC,L]

It's debatable whether you should be catching both /downloads and /downloads/ - ideally, it should be one or the other.

To trap a specific URL (eg. /downloads/somedownload), then this would need to go before the "Everything else..." rule:

RewriteRule ^downloads/somedownload$ /somePackage/index.php?id=5 [NC,L]
MrWhite
  • 13,016
  • ok that was me being smart... but still without the condition it works for downloads but not for downloads/example in this case i don't even need a condition i think – Kevin Kloet Oct 31 '16 at 09:47
  • I've updated my answer. Your RewriteRule downloads/? .... would match both of those URLs. But what exactly are you trying to do - what URLs are you trying to match? – MrWhite Oct 31 '16 at 10:20
  • i want to achieve when the url is localhost:8080/downloads the user get's the localhost:8080/somepackage/index.php?id=2 in this i have url's to for example: localhost:8080/downloads/somedownload however when i do rewrite the rule it gives a 404 - Category not found but when i do localhost:8080/downloads/ it redirects to the rewrite rule of RewriteRule downloads/.*/? /somePackage/index.php?id=5 [NC,L]. how would i get this to work for downloads/somedownload? – Kevin Kloet Oct 31 '16 at 10:24
  • It's still not clear from your comment... what should localhost:8080/downloads/somedownload rewrite to? Do these all rewrite to different URLs? Should /downloads go to id=2 and /downloads/<everything-else> go to id=5? – MrWhite Oct 31 '16 at 11:10
  • basically i want localhost:8080/downloads to go to id=2 and localhost:8080/downloads/somedownload to go to id=5 and localhost:8080/downloads/anotherdownload to go to id=6 however for the question i have compacted it to /downloads/<everything-else> go to id=5 – Kevin Kloet Oct 31 '16 at 11:18
  • this seems to give me the same problem, i have put your code in the .htaccess, agian the /downloads works but the /downloads/somedownload still gives 404 - Category not found, the id=5 exists because i tried changing the /download to this and this worked. but why does the /download/somedownload not work? what am i doing wrong? i can't seem to figure it out. – Kevin Kloet Oct 31 '16 at 12:16
  • If /somePackage/index.php?id=5 is a valid URL, and works when accessed directly (or by changing 2 to 5 in the first rule, as you seem to have tried) then it would seem /downloads/somedownload is not matching the required RewriteRule pattern. What is the exact URL and directive you are using? – MrWhite Oct 31 '16 at 12:21
  • i'm using this url to get to the page: http://localhost:8080/downloads/ignis_desktop

    also accessing the url directly works.

    also i'll add the .htaccess code in the question.

    – Kevin Kloet Oct 31 '16 at 12:26
  • It does look as if it should work. Try (temporarily) making this into an external redirect by changing the flags on that directive from [NC,L] to [R,NC,L]. Are you redirected? ie. Do you see http://localhost:8080/somePackage/index.php?id=5 in the browser's address bar? – MrWhite Oct 31 '16 at 12:32
  • Are you redirected? no. Do you see http://localhost:8080/somePackage/index.php?id=5 in the browser's address bar? no. it seems like the it doesn't get in the rewriteurl, but why? – Kevin Kloet Oct 31 '16 at 12:36
  • Just to clarify, /downloads is not a physical directory on the filesystem is it? – MrWhite Oct 31 '16 at 12:38
  • no /downloads is not a physical directory, however /somePackage/download is one, but that one is never called in the .htaccess. – Kevin Kloet Oct 31 '16 at 12:44
  • update: after clearing cache the redirect seems to work with the [r=301] flag. the url is indeed http://localhost:8080/somePackage/index.php?id=5 and the content of this page is correct, but why doesn't it work without the [r=301] flag? – Kevin Kloet Oct 31 '16 at 12:50
  • Don't use r=301 (ie. a permanent redirect) to test this, it should simply be R (or R=302). Permanent redirects are cached by the browser, which could have caused problems earlier. If the redirect is working, then the rewrite should also now work - but you will need to clear all client-side and intermediary caches before testing if you have used a 301. – MrWhite Oct 31 '16 at 14:24
  • changed to [r] cleared all caches, the redirect works but the rewrite doesn't, it still gives me 404 - category not found – Kevin Kloet Oct 31 '16 at 14:28
  • That's a bit of a mystery - it doesn't really make sense. When you added the R flag, did you keep the other flags, ie. [R,NC,L] - importantly the L flag? Since [r] is quite different to simply adding R. Without the L flag the rewriting process would continue onto the next directive. – MrWhite Oct 31 '16 at 15:28
  • yes i kept those in like this: [R,NC,L] i don't know what is the cause of the 404 – Kevin Kloet Oct 31 '16 at 15:30
  • Presumably, if you comment out that directive then the following "Everything else" directive gets triggered (which also goes to the same category id=5)... does this "work" or give the same 404? – MrWhite Oct 31 '16 at 15:36
  • the "everything else" rule also gives me the 404 message, is there any chance that the .htaccess file thinks that the ^downloads/<everything else> is not a valid url and gives me the 404? what else could be the cause of this problem? – Kevin Kloet Oct 31 '16 at 15:40
  • That is very probable - although not .htaccess. Your site code would seem to be what's generating the 404 - not .htaccess. (I had previously discounted this as the URL /downloads appeared to work OK - although this is perhaps a "valid" URL which allows it to "work"?) – MrWhite Oct 31 '16 at 15:46
  • /downloads is an url specified to rewrite in the .htaccess to the somePackage/index.php?id=2 page is this the cause of the error? i don't think it would cause this because we specify downloads/<something> – Kevin Kloet Oct 31 '16 at 15:50
  • That can't cause this error. (But you tried changing the 2 in that substitution to 5 and that "worked" OK?) – MrWhite Oct 31 '16 at 16:02
  • when i change it to 2 i get an empty page instead of the /download page, i also have id=6 and that also gives me 404 - Category not found although when i directly access it the page works great and when i try with the [r] flag it also works great. but not without the redirect thus breaking the whole purpose of the rewriterule – Kevin Kloet Nov 01 '16 at 07:08
  • I have found the solution to my problem, – Kevin Kloet Nov 01 '16 at 07:24
0

i have found the solution to my problem,

i have changed the rewrite to this:

RewriteRule ^downloads/somedownload$ /somepackage/index.php?option=com_content&view=article&id=5

although in my menu i can refer to the pages like this:

localhost:8080/home

and it would rewrite it to:

RewriteRule ^home/?    /somepackage/index.php?id=1   [L,NC]