Solved

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

Posted on 2010-09-01
7
333 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
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 video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to dynamically set the form action using jQuery.

861 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