The backslash problem

I tried a lot of things, I really did! I am not so fluent :frowning:

Is there a way so that, page/ does to page and never has a trailing slash?

Options +FollowSymLinks
AddDefaultCharset UTF-8

RewriteEngine On
RewriteBase /beta/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !\\.(css|jpg|gif|png|js)$


RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

How do you expect that .htaccess to remove trailing slashes if it doesn’t contain slashes in the rule at all? :shifty:

You need to add another rule stating that if an URL ends in a / , rewrite to that exact same URL without the slash. So, capture the part without the slash, and redirect to the thing you captured.
Sound too abstract? Alright, it looks something like this:


RewriteRule ^(.*)/$ $1 [L,R=301]

:slight_smile:

JR,

If that is ALL your .htaccess file, you should NOT be using RewriteBase (it’s intention is to correct a Redirect so mod_rewrite can work on the URI).

If page is a directory, it SHOULD have a trailing /. Removing the trailing / only makes Apache go figure out that it’s a directory THEN select the DirectoryIndex (two silly things to make Apache do).

What you’ve shown does NOT even attempt to remove a trailing /. If that’s what you want to do, please show what you’ve attempted - it’s better to LEARN what changes need to be made rather than spoonfeeding (which I won’t do) code.

Regards,

DK

Hey guys, LOL no spoon-feeding. I tried a few things (I don’t have all my mistakes and edits since I Don’t backup the .htaccess, however some of what i did is below).

I agree with the folder / logic, however, the reason I wanted such a thing is because facebook comment connections saves info into their system based on the URL, so a URL with a / and without one are totally different, and the results won’t display the same on both.


DirectorySlash Off # This caused some troubles (mod_dir was on)

RewriteEngine On
RewriteBase /beta/ # I did this since its in a sub-directory, i guess i dont need this, embarassing!

Some things I’ve tried also are:


RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] 
RewriteRule ^(.*)$ index.php?url=$1/ [QSA,L] # Practically same thing, tried forcing the slash on alternatively


# I know this is wrong, but can't fix it 
RewriteCond %{HTTP_HOST} !^\\.swfup\\.com/beta$ [NC] 
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^(.*)$ index.php?url=$1/ [QSA,L]

By the way can anyone suggest a good Apache book on this stuff? I seem to have so many problems with this I might as well study it better (On paper, I can’t study online very well). I’m not smart enough to be a System Administrator, but I a book on a few of the popular Apache mods would go a long ways. I seem to remember shortly, then forget, and easily screw things up LOL.

Hi JR,

Okay, so long as you understand the folder logic, let’s get on with your problem.

Is the link a file or a directory? “They’re not the same and won’t display the same on both.” :lol: Sorry, I couldn’t resist! :blush:

Let’s go through your code:


DirectorySlash Off # This caused some troubles (mod_dir was on)
[indent]Thanks for the pointer on this module! 
[quote="apache.org"]
The DirectorySlash directive determines, whether mod_dir should fixup URLs pointing to a directory or not.

Typically if a user requests a resource without a trailing slash, which points to a directory, mod_dir redirects him to the same resource, but with trailing slash for some good reasons:

    * The user is finally requesting the canonical URL of the resource
    * mod_autoindex works correctly. Since it doesn't emit the path in the link, it would point to the wrong path.
    * DirectoryIndex will be evaluated only for directories requested with trailing slash.
    * Relative URL references inside html pages will work correctly.

Well, if you don't want this effect and the reasons above don't apply to you, you can turn off the redirect with:

# see security warning below!
<Location /some/path>
DirectorySlash Off
SetHandler some-handler
</Location>

[B][COLOR="Red"]Security Warning[/COLOR][/B]

Turning off the trailing slash redirect may result in an information disclosure. Consider a situation where mod_autoindex is active (Options +Indexes) and DirectoryIndex is set to a valid resource (say, index.html) and there's no other special handler defined for that URL. In this case a request with a trailing slash would show the index.html file. But a request without trailing slash would list the directory contents.

[/quote]

:blush: Nice to have Apache verify (and amplify) my warning![/indent]
RewriteEngine On
RewriteBase /beta/ # I did this since its in a sub-directory, i guess i dont need this, embarassing!

DON’T be embarassed! Even the folks at WordPress use this BECAUSE they need to protect against any Redirect statements they can’t be aware of!


# REDIRECTS EVERYTHING to index.php - including index.php
# the result will be index.php?url=index.php
# If you've ever wondered why I "go off" about (.*), this is it!
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] 
# Adding a / to the query string is, well, ridiculous!
RewriteRule ^(.*)$ index.php?url=$1/ [QSA,L] # Practically same thing, tried forcing the slash on alternatively

Okay, knowing that it’s a “social” website that’s driving this helps but I fear that I need to see an example of the URI that you’re trying to use - as part of the need for “Specificity.”


# I know this is wrong, but can't fix it 
RewriteCond %{HTTP_HOST} !^\\.swfup\\.com/beta$ [NC] 
# a directory is NEVER part of the {HTTP_HOST} variable => false
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
# will NEVER be redirected because of the directory in the RewriteCond
RewriteRule ^(.*)$ index.php?url=$1/ [QSA,L]
# same LOOPY problem as above except that it adds a trailing / to the QUERY STRING!

Book? If you are looking for SysAdmin info, Dan Bowen’s The Definitive Guide to Apache mod_rewrite is excellent - and it’s been updated for Apache 2. If you understand the differences between putting your mod_rewrite code in server or VirtualHost configuration files and .htaccess, you’ll do well with this one.

Specificity, if you please. At least, please give us an example of the link you’re attempting to deal with!

Regards,

DK

I’ll second this, though it’s a SMALL book, and cost a lot (maybe because they only had it in hardcover? lawlz). It basically is an “English” version of the Apache Docs but with some added examples (which were essential). He does assume everything’s done directly in the config file, not .htaccess.

First off, thanks for taking time to comment on this you guys. I know technical things aren’t just whipped out like a conversation, and take some analyzing.

Second, this book is a bit pricey ($40) for my budget, lol. Perhaps I’ll wait or pick an alternative.

Third, I’ll go through this code in a day or two (I’m slow) and I actually print it out to read instructions. I don’t know why but I can’t read a ton of instructions on the screen well, I need to sit outside with a highlighter, LOL. (Unless it’s like a few lines of code Im familiar with, or its easy)

I like Apache but for some reason the way everything is written and structured is different from anything else I’ve used, It’s not like an INI/CONF file for PHP or HTTPD. I’ve been playing with VHosts on Apache 2 a little while on my Ubuntu, I’ve tried my own Cloud Server a few times, it’s fun to play with but man you really gotta know what you’re doing, if I ever changed a setting and screwed anything up, oh boy. LOL

Here is an example URL: (I only need one or the other working)

http://swfup.com/beta/view/1291354159
http://swfup.com/beta/view/1291354159/

Wow I never knew this would happen… I thought $(.+) was basically one character, plus another… So I used an asterisk LOL.

Okay that makes sense, but I thought you’d be able to append to it. I think my thinking is totally off course, the more I think about the HTTP_HOST part the more confused I get, can’t you just say… rewrite everything to http://{HTTP_HOST}/a-directory/$url-here

Apache has no mercy!! :stuck_out_tongue:

Also, in general wouldn’t it be smart to do this, since I only want to redirect PHP pages?


RewriteRule ^(*.php)$ index.php?url=$1 [QSA,L]

Regards,

DK

Uh, I remember something about adding a ? afterwards to make a regex lazy/ungreedy. Does that hold for Apache?

Not so much for the above, since it’s generally better to be specific anyway, but I mean for when it seems you really do need a *.

Mallory,

Is it Perl regex? YES! But, as you noted, greedy or not, using the end anchor makes it finish the text of the {REQUEST_URI} variable.

Regards,

DK

I gotta re-read some more, bad week got hacked and quit my jobs LOL

JR,

Bummer! I hope you got their IP Address and reported the hacking to the FBI.

Regards,

DK

Can you really send that to the FBI? They are from another country. I have to take security much more seriously, this would be bad if I was a successful business! Anyways!

So would it be better to do…

RewriteRule ^([a-zA-Z0-9_-.)$ index.php?url=$1 [QSA,L]

With rewriting digits and regular URL’s, is it best practice to try and handle everything from one main .htaccess file, or should I put other handling rewrite’s in sub-directories? (If I had subdirectories)

JR,

If they are from a different country, report it to ICAAN - ANYBODY that would/SHOULD care and be able to do something about it!

RewriteRule ^([[COLOR="Red"]-[/COLOR]a-zA-Z0-9_.]+)$ index.php?url=$1 [QSA,L]

Remember that placing a - inside a character range definition makes it a metacharacter and the only way to have it interpreted as a - is to make it the first character (or the last character but first is preferred). Further, though, you need to CLOSE the character range definition and specify how many character you’ll accept. Finally, if it’s the URL you want, all you will match (without the / in the character range definition) is a filename in the root directory (note the start and end anchors).

As for your .htaccess location question, I prefer to keep everything which applies to the domain in the DocumentRoot’s .htaccess (it’s easier for me to get my head around it that way).

Regards,

DK

I’m about lost in all this server stuff, haha… I’ll keep practcing and Im going to find a good mod rewrite book… I can usually poke around and figure stuff out, for some reason the webserver administration is something you can’t test and learn as easily – not for me anyways.

Remember that placing a - inside a character range definition makes it a metacharacter and the only way to have it interpreted as a - is to make it the first character

Ah I didnt realize that!!! Good call, I think you could escape it maybe too ?? \-

But i wont keep asking questions and keep you slave attached to this thread LOL

JR,

Well, may I recommend the mod_rewrite tutorial ARTICLE linked in my signature?

No, you DON’T escape the - in a character range definition. It’s a metacharacter UNLESS it can’t be confused with a metacharacter, i.e., first or last.

I’m here to help you learn - the more questions from you, the more you learn. Just as importantly, others too bashful to ask the questions themselves will read and learn, too! To me, that’s just as important … so ask away!

Regards,

DK