Solved

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

Posted on 2010-09-01
7
336 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

707 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