Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite problem.

         

Saturn

11:31 pm on Aug 1, 2007 (gmt 0)

10+ Year Member


Hi, I am developing locally and using xampp. But the same httpd.conf is on the webhost server, so I think it's not my httpd.conf.

Okay, my problem is the following:

http://localhost/dev/main <-- works
http://localhost/dev///////main <-- works also...

No matter how many slashes (/) I add, the url will still work.
I have tried to get rid of this and make only one slash work in my URI.
My .htaccess
-----------------------------------------------

RewriteEngine On
IndexIgnore *

RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d

RewriteRule ^main$ index.php?page_id=main [L]
RewriteRule ^search$ index.php?page_id=search [L]
RewriteRule ^articles/$ index.php?page_id=page_id.archive [L]
RewriteRule ^articles/(\d+)$ index.php?page_id=articles&art=$1 [L]
RewriteRule ^articles/(\d+)&pid=(\d+)$ index.php?page_id=articles&art=$1&pid=$2 [L]
------------------------------------------------------------

And so on.
I just want my pages to work http://localhost/dev/blah/bleh/bloh etc...
And when added extra slashes, they won't work.

Any idea please?

[1][[b]edited by[/b]: Saturn at 11:32 pm (utc) on Aug. 1, 2007][/1]

jdMorgan

12:16 am on Aug 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The multiple-slash handling is the default Apache behaviour. You can force a redirect if you like.

Be aware that RewriteConds only apply to the *single* rule that follows them. I have removed them because they don't seem necessary in these rules that affect only specific pages.

I'd suggest:


RewriteEngine on
#
# Redirect to remove multiple slashes
RewriteCond %{REQUEST_URI} ^(.*)//+(.*)$
RewriteRule / http://www.example.com%1/%2 [R=301,L]
#
# If page does not exist, rewrite request to index.php with page name in page_id query parameter
RewriteRule ^(main¦search)$ index.php?page_id=$1 [L]
#
# Rewrite /articles/ request to index.php?page_id=page_id.archive
RewriteRule ^articles/$ index.php?page_id=page_id.archive [L]
#
# Rewrite /articles/<numbers> request to index.php?page_id=articles&art=<numbers>
# and [b]retain any existing query parameters[/b] (such as pid=nnn)
RewriteRule ^articles/([0-9]+)$ index.php?page_id=articles&art=$1 [QSA,L]

Here's an alternate coding of the final rule above to demonstrate mod_rewrite query-string handling.

# Rewrite /articles/<numbers>?pid=<number> request to index.php?page_id=articles&art=<numbers>&pid=<number>
# (Note the special construct in the RewriteCond which allows this to work even if no pid is present.)
RewriteCond &%{QUERY_STRING} ^(&pid=[0-9]+)$
RewriteRule ^articles/([0-9]+)$ index.php?page_id=articles&art=$1%1 [L]

Replace the broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.
Flush your browser cache completely before testing modified code.
"\d" changed to "[0-9]" for best code portability between different Apache OS ports.

Jim

[edited by: jdMorgan at 1:22 am (utc) on Aug. 2, 2007]

Saturn

1:07 am on Aug 2, 2007 (gmt 0)

10+ Year Member


Hi Jim,

I am trying to work it out but.

RewriteCond %{REQUEST_URI} ^(.*)//+(.*)$
RewriteRule / http://localhost/dev %1/%2 [R=301,L]
( I need to use localhost as I am working locally for now.

I get this error for
http://localhost/dev/main
------------->

Server error!

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.

If you think this is a server error, please contact the webmaster.
Error 500
localhost
08/02/07 04:02:39
Apache/2.2.0 (Win32) DAV/2 mod_ssl/2.2.0 OpenSSL/0.9.8a mod_autoindex_color PHP/5.1.1

jdMorgan

1:18 am on Aug 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, sorry, it's a typo... I typed an extra space in this line:

RewriteRule / http://www.example.co[b]m%[/b]1/%2 [R=301,L]

I'll fix it above too, so no-one copies the bad code.

Jim

[edited by: jdMorgan at 1:23 am (utc) on Aug. 2, 2007]

Saturn

1:41 am on Aug 2, 2007 (gmt 0)

10+ Year Member



Hello Jim,

Thanks. That works in cases like
[localhost...]
Or
[localhost...]

But I am still having a small problem


If I type an extra slash (/) the URL is like this ->
http://localhost/dev//dev/dev/dev/dev/dev/dev/dev/dev/dev/dev/dev/dev/dev/dev/dev/dev/dev/dev/dev/dev/paste/

Anyway I can make it work for URLS like http://localhost/dev/contact <--?
Also, can this force redirect take me to http://localhost/main when an extra slash is added? Or remove the extra slashes from the URL so http://localhost/dev//contact - become -> http://localhost/dev/contact automatically?

Thanks in advance

Small edit: I figured out myself that if I use (RewriteRule / [localhost...] [R=301,L] ) double slashes will be redirected to the main index. But still, [localhost...] & [localhost...] or [localhost...] are not redirected.
now it has become about the double slashes before the page name, means the double slash after /dev/

Note (if this make any importance): when this website is done, it will be directly [domain-name-here.org...]

[edited by: Saturn at 1:56 am (utc) on Aug. 2, 2007]

jdMorgan

2:06 am on Aug 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't think this will work if you add any path info (such as /dev) to the substitution URL. It will work exactly as I showed it above -- It's what I use myself.

Jim

Saturn

2:14 am on Aug 2, 2007 (gmt 0)

10+ Year Member



U'huh

I tried that without using path info.

RewriteCond %{REQUEST_URI} ^(.*)//+(.*)$
RewriteRule / http://localhost/ [R=301,L]

#Statis pages
RewriteRule ^me$ slashme.php [L]
RewriteRule ^loop$ loop.php [L]

Then [localhost...] and [localhost...] both work.