Forum Moderators: phranque

Message Too Old, No Replies

Can't get .htaccess RewriteRule right

.htaccess mod_rewrite rewriterule

         

evn64

10:12 am on Feb 6, 2011 (gmt 0)

10+ Year Member



I'm trying to figure this out now for two days, without any success. This is the currrent situation:
I have a Joomla website with SEF and mod_rewrite turned on. This results in the URL:
mysite.com/index.php?option=com_remository&Itemid=7
being rewritten to:
mysite.com/sub-directory/sub-directory/0000-Business-files/

These are the lines that are currently used in my .htaccess (all standard Joomla)

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^([^\-]*)\-(.*)$ $1 $2 [N]

RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]

# RewriteBase /


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]


This is what I want to achieve:
When a visitor uses this URL
mysite.com/sub directory/sub directory/0000 Business files/
it should lead him to the right page.

I found this example on this forum for replacing spaces for hyphens.
# Replace spaces with hyphens
RewriteRule ^([^\ ]*)\ (.*)$ $1-$2 [E=rspace:yes,N]
# Redirect to update URL in search engine listings and browsers
RewriteCond %{ENV:rspace} yes
RewriteRule (.*) http://%{HTTP_HOST}$1 [R=301,L]


I need to go just the other way. I've added this rule on top of the .htaccess file:
RewriteRule ^([^\-]*)\-(.*)$ $1 $2 [N]


But all I get is 'page not found'. Is the code wrong? Or, also possible, am I misconceiving the use of mod_rewrite?

gr, evn64

g1smd

5:00 pm on Feb 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When a user uses spaces do you want the content to be delivered "at" that URL, or do you want to redirect the user to the right URL, one with hyphens? The latter approach is preferred.

The redirect code MUST be listed before any of the rewrite code.

The Joomla code you have is much less than efficient. There's replacement code to be found in another thread here.

Additionally, there's a patch that has been waiting 5 months for review over at: [joomlacode.org...]

evn64

10:08 am on Feb 7, 2011 (gmt 0)

10+ Year Member



Thanks g1smd for replying.

If possible I want the user to the right URL, the hyphened one. Thank you for the improved code for Joomla. I'll replace it. Strange though that even when an improved version is available, the latest version of Joomla still uses the old one.

About the redirect code before the rewrite code, that's when I realise I'm not yet op top of this matter. I think I have to do some additional studying to get a grasp of this matter.
gr, evn64

g1smd

9:19 pm on Feb 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks, and do report any bugs or problems in the new code.

jdMorgan

10:39 pm on Feb 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In modifying my space-to-hyphen code, it's quite important to include all three directives; change only the regular-expressions pattern and the substitution, and keep the logic structure as originally posted...

Jim

evn64

12:54 am on Feb 8, 2011 (gmt 0)

10+ Year Member



Thank you, jdMorgan,

It was indeed your code that I've used in my attemp to solve my puzzle. I'll report on my progress and perhaps have some additional questions. Perhaps it's helping out someone else. I must say it's quite a hard nu to crack so far.

with rgds, evn64

ewanvrooyen

4:06 pm on Feb 9, 2011 (gmt 0)

10+ Year Member



My redirects work except for urls with index.php?

All my other urls are redirecting using the 301 redirect text i.e:
Redirect 301 /Jobs/job-north-wales-housing [mysite.org.uk...]

Urls with the "?" after index.php goes to the homepage even when using the Redirect 301:
Redirect 301 /index.php?page=useful-contacts-websites&hl=GBR [nwha.org.uk...]

I hope this is not off topic.

My .htaccess file looks like this:

+++++++++++++++++++++++++++++++++++++++++++++++

## Can be commented out if causes errors, see notes above.
Options +FollowSymLinks
#
# mod_rewrite in use

RewriteEngine On

########## Begin - Rewrite rules to block out some common exploits
## If you experience problems on your site block out the operations listed below
## This attempts to block the most common type of exploit `attempts` to Joomla!
#
## Deny access to extension xml files (uncomment out to activate)
#<Files ~ "\.xml$">
#Order allow,deny
#Deny from all
#Satisfy all
#</Files>
## End of deny access to extension xml files
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
# Block out any script trying to base64_encode crap to send via URL
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
# Block out any script that includes a <script> tag in URL
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Send all blocked request to homepage with 403 Forbidden error!
RewriteRule .* index.php [F]
#
########## End - Rewrite rules to block out some common exploits

# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root)

# RewriteBase /


########## Begin - Joomla! core SEF Section
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} (/(component/)?|\.(php|html?|feed|pdf|raw)|/[^.]*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
#
########## End - Joomla! core SEF Section

########## ITECH - Redirect old pages
#
## redirect non-www to www
#
rewritecond %{http_host} ^nwha.org.uk [nc]
rewriterule ^(.*)$ [nwha.org.uk...] [r=301,nc]
#
##
#
## WORKING redirect links
#
Redirect 301 /index.php/GBR/contact-us [nwha.org.uk...]
#
##
#NOT redirecting
##
Redirect 301 /index.php?page=useful-contacts-websites&hl=GBR [nwha.org.uk...]
#
########## END ITECH - Redirect old pages

g1smd

6:32 pm on Feb 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As coded now, you have external redirects placed after internal rewrites. Those redirects may expose internally rewritten URLs back out on to the web.

List all of the redirects BEFORE the internal rewrite code.

Change all of the rules that use Redirect or RedirectMatch to instead use RewriteRule code. Do not have a mix of directive types, use RewriteRule for all of the rules.

Redirect, RedirectMatch and RewriteRule cannot see query string data attached to the URL. You must use a RewriteCond looking at QUERY_STRING to see the query string data, and that RewriteCond should be placed immediately before the RewriteRule that it applies to.

Make sure that every RewriteRule ends with the [L] flag.

evn64

9:09 pm on Feb 27, 2011 (gmt 0)

10+ Year Member



Grr... this mod_rewrite is driving me insane.

I defintily start to think that it was never ment to be that I would write, or even alter my own .htaccess file.

This is what I got so far:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]


And it works. Of course it does, because this is the standard .htaccess from Joomla. I stripped out all the parts until it stopped working. Just to get the simpelest file and I now have a URL that's originally:
www.myserver.com/index.php?option=com_remository&Itemid=8&func=fileinfo&id=10

with SEF URLs and Use Apache mod_rewrite on it gets rewritten to
www.myserver.com/archief/documenten/K23001/fotos/Afbeelding-016.jpg/

A last step is needed because hyperlinks to these pages are already in a PDF.

In the PDF this is the link:
www.myserver.com/documenten/K23001/fotos/Afbeelding 016.jpg

So documenten should be rewritten as archief/documenten and Afbeelding 016.jpg as Afbeelding-016.jpg/

But I don't even get the first part working :((
I thought a simple
RewriteRule ^documenten$ archief/documenten [NC]
should work.

These are the result of different URL's
www.myserver.com/documenten/K23001/fotos/Afbeelding-016.jpg/
-> 404-component not found.

www.myserver.com/documenten/K23001/fotos/Afbeelding-016.jpg
-> The requested URL /documenten/K23001/fotos/Afbeelding-016.jpg was not fount on this server.

www.myserver.com/archief/documenten/K23001/fotos/Afbeelding-016.jpg/
-> this one works

www.myserver.com/archief/documenten/K23001/fotos/Afbeelding-016.jpg
-> The requested URL archief/documenten/K23001/fotos/Afbeelding-016.jpg was not fount on this server.
Can someone help me out here or point me in the right direction?

I would really, really appreciate this!

rgds, evn64

g1smd

10:24 pm on Feb 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I now have a URL that's originally:
www.myserver.com/index.php?option=com_remository&Itemid=8&func=fileinfo&id=10

with SEF URLs and Use Apache mod_rewrite on it gets rewritten to
www.myserver.com/archief/documenten/K23001/fotos/Afbeelding-016.jpg/

And that, right there, is the problem you are having.

Things do not get rewritten to a URL.

The rewrite does not "make" a URL.

What actually happens is that mod_rewrite accepts an incoming request for the URL
www.example.com/archief/documenten/K23001/fotos/Afbeelding-016.jpg/
and rewrites it to fetch content from the server internal filepath at
/index.php?option=com_remository&Itemid=8&func=fileinfo&id=10
.

That is, mod_rewrite does not "make" URLs for content. URLs are defined in links. Once the link is clicked, the browser sends a URL request to the server. The server either fetches content from the path hinted at in the URL request OR the request is rewritten to fetch the content from some other place inside the server.

Alternatively, mod_rewrite responds with a redirect, telling the browser to make a new request for a different URL.

Knowing when to use a redirect and when to use a rewrite is crucial in getting this code to work. Until you are 100% clear on those points, it is fruitless to start any coding.

evn64

12:40 am on Feb 28, 2011 (gmt 0)

10+ Year Member



Thank you g1smd! You've been a great help.

It all becomes clear now. I totally misconceived the principle of mod_rewrite.

But then again, with the risk of still being completely wrong, if I understand it correctly, then shouldn't it be possible that mod_rewrite accepts an incoming request
www.example.com/documenten/K23001/fotos/Afbeelding-016.jpg
to rewrite so the internal filepath
/index.php?option=com_remository&Itemid=8&func=fileinfo&id=10
gets fetched?

(Whereby the step from 'documenten' to 'archief/documenten' is not a rewrite but a redirect)

If I still got it wrong, I'll will hire a programmer to do this for me. Some things you can do yourself, but some things you don't... (plastering a wall comes to mind here...)

rgds, evn64

g1smd

2:28 am on Feb 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, you can program mod_rewrite to internally rewrite a URL to a filepath

RewriteRule ^urlpath /serverfilepath [L]


or you can set it to issue an external redirect to a new URL

RewriteRule ^urlpath http://www.example.com/newpath [R=301,L]

evn64

10:52 am on Feb 28, 2011 (gmt 0)

10+ Year Member



Hi g1smd,

with the insight I've got by your explanation, I'm now pretty sure the setup I had in mind, couldn't have worked. So I've changed plans and thought up another solution. Unfortunately I still think it needs some .htaccess modification. But I think this is pretty straight forward.

Is it possible to redirect a non-registered joomla user to the loginpage (or any other URL) when his URL starts with:
'http://www.myserver.com/documents'?

What I mean is: is it possible to setup an RewriteCond and RewriteRule based on if your logged in or not into Joomla?

Or is this more a question to ask in a Joomla Forum?

rgds, evn64

g1smd

8:03 pm on Feb 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't know what information you need to detect at the .htaccess level to know if a user is logged in or not. If it is a static cookie value it might be simple. However, NOTHING in Joomla is ever simple, so I suspect not.

evn64

8:20 am on Mar 1, 2011 (gmt 0)

10+ Year Member



Hi g1smd,

You were right. It became to complicated to solve this way. So after some thinkin about the options available, I solved it this way:
- the documents resides in a folder in the root of myserver.com
- the folder is protected by .htaccess and .htpaswrd
- the content of the folder is listed after login by an extension that does just that: list files and make them linkable.

Much, much more easier. No worries about rewriting or redirecting.

For everybody that anwsered my questions: thank you. You we a big help in discovering the (im)posibilities of mod_rewrite.

rgds, evn64

g1smd

9:17 am on Mar 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No problem! Although mod_rewrite is very powerful, there a small number of things that are just not possible to do efficiently, or at all.

FYI, there are now patches for Joomla 1.5 and Joomla 1.6 to update the .htaccess file. If you would like to test the appropriate file out, then go ahead.

The latest file in each case is the one ending .3.patch. They are at: [joomlacode.org...] (1.5) and [joomlacode.org...] (1.6).