Solved

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

Posted on 2010-09-01
7
331 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
ID: 33576084

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
ID: 33576263
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
ID: 33601518
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 30

Expert Comment

by:Brad Howe
ID: 33601925
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
ID: 33626592
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
ID: 33708041
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
ID: 33734228
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
"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 demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

863 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

19 Experts available now in Live!

Get 1:1 Help Now