Solved

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

Posted on 2010-09-01
7
332 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
This code tracks birthdays 3 72
wamp versus xampp 4 58
Advice on Xojo as a development tool over VB. 4 53
Excel Hyperlink Question 4 48
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
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 learn how to count occurrences of each item in an array.

786 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