Solved

htaccess help - need to redirect urls with invalid caracters to homepage

Posted on 2010-09-01
7
328 Views
Last Modified: 2012-06-27
I have the following ReWrite that I am happy with and works;
RewriteCond %{QUERY_STRING} ^param=([a-zA-Z0-9_-]*)$
RewriteRule ^index\.php$ /ff/%1? [R,L]

what I want is ReWrite rule that redirects any urls, with a parameter not matching above [a-zA-Z0-9_-] to go to the home page /ff/

i.e.
/ff/index.php?param=aaa'bbbb
/ff/index.php?param=aaa*bbbb
/ff/index.php?param=aaa+bb(bb
/ff/index.php?param=a^aa@bb#bb

would all redirect to  /ff/
0
Comment
Question by:thydzik
  • 4
  • 2
7 Comments
 
LVL 30

Assisted Solution

by:Brad Howe
Brad Howe earned 250 total points
Comment Utility

Why not just reverse it. The ! is the negation of a condition.  So 

# Condition GOOD, forward.
RewriteCond %{QUERY_STRING} ^param=([a-zA-Z0-9_-]*)$
RewriteRule ^index\.php$ /ff/%1? [R,L]

#ELSE Condition BAD Homepage
RewriteCond %{QUERY_STRING} !^param=([a-zA-Z0-9_-]*)$
RewriteRule ^index\.php$ /ff/ [R,L]

Cheers, Hades666
0
 
LVL 11

Author Comment

by:thydzik
Comment Utility
this sounded good. but the problem is it loops for all root sites (/ff/) but also all /ff/aaaabbbb derivatives.

note, i have added the '?' the the end to stop the parameter string showing ie.

RewriteCond %{QUERY_STRING} !^param=([a-zA-Z0-9_-]*)$
RewriteRule ^index\.php$ /ff/? [R,L]
0
 
LVL 26

Assisted Solution

by:arober11
arober11 earned 250 total points
Comment Utility
You could Just change the "*" (Zero or more) to a "+" (One or more)


RewriteCond %{QUERY_STRING} !^param=([a-zA-Z0-9_-]+)$
RewriteRule ^index\.php$ /ff/? [R,L]


OR logically the same:

RewriteCond %{QUERY_STRING} ^param=[a-zA-Z0-9_-]*[^a-zA-Z0-9_-]+
RewriteRule ^index\.php$ /ff/? [R,L]
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:Brad Howe
Comment Utility
Hi,
Off to bed at the moment but could you not try to use (\W)+ as your regular expression or a variation of

\w Any word character [a-zA-Z0-9_]
\W Any non-word character [^a-zA-Z0-9_]

It will only accept not A-Z0-9a-z_ characters.

eg: This is not tested but something like this.
RewriteCond %{QUERY_STRING} !^param=[^a-zA-Z0-9_=-]+$
RewriteRule ^index\.php$ /ff/? [R,L]

-Hades666
0
 
LVL 11

Author Comment

by:thydzik
Comment Utility
guys, apologies for letting this sit. give me a week and a bit more before I can try these out.
0
 
LVL 11

Author Comment

by:thydzik
Comment Utility
okay, still can't get this to work, here is all my htaccess;

# The following redirects to a nice looking url whenever a value is searched

RewriteCond %{QUERY_STRING} ^param=([a-zA-Z0-9_-]*)$

RewriteRule ^index\.php$ /ff/%1? [R,L]



# The following cathes everything else and redirects to the homepage

RewriteCond %{QUERY_STRING} ^param=[a-zA-Z0-9_-]*[^a-zA-Z0-9_-]+

#RewriteRule ^index\.php$ /ff/? [R,L]



# The following rule writes any nice url to the search variable

RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?param=$1&a [L]

Open in new window

0
 
LVL 11

Accepted Solution

by:
thydzik earned 0 total points
Comment Utility
okay, I did this in a dodgy way, but it works.

Any invalid characters cause a 404 not found,
and I redirect all 404 errors to the index page.
ErrorDocument 404 http://thydzik.info/ff

# The following redirects to a nice looking url whenever a value is searched
RewriteCond %{QUERY_STRING} ^param=([^&]*)$
RewriteRule ^index\.php$ /ff/%1? [R,L]

# The following rule writes any nice url to the search variable
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?param=$1&a [L]

Open in new window

0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now