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,315
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]