LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 12-13-2010, 04:17 PM   #1
konzo
Member
 
Registered: Dec 2008
Location: Bulgaria
Distribution: Debian / Slackware / Ubuntu
Posts: 230

Rep: Reputation: 34
Apache rewrite regex


Hi all,

I have simple blog I created myself. I am using mod rewrite to redirect all to a php file. But I have links in google like kdelchev.com/?p= or kdelchev.com/?cat=7

How can I match it with regular expression? I try with variants of ^([^/\?]+)/$ but with no success.

The desired result is to redirect such addresses to kdlechev.com or to 301 page
 
Old 12-13-2010, 05:58 PM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Hi,

You can use the following:
Code:
RewriteEngine on

RewriteCond %{QUERY_STRING} ^(.*)=
RewriteRule ^(.*)$ /? [R=301]
Regards
 
1 members found this post helpful.
Old 12-13-2010, 06:21 PM   #3
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
It also helps if you can see what's going on, try turning on logging -

Code:
RewriteLog /var/log/httpd/mysite_rewrite.log
RewriteLogLevel 3
cheers
 
1 members found this post helpful.
Old 12-14-2010, 02:34 AM   #4
konzo
Member
 
Registered: Dec 2008
Location: Bulgaria
Distribution: Debian / Slackware / Ubuntu
Posts: 230

Original Poster
Rep: Reputation: 34
One more question: How can I redirect all invalid addresses like http://kdelchev.com/?cat=7 to http://kdelchev.com/ i.e. to match everything that begin with ? after the /
 
Old 12-14-2010, 02:52 AM   #5
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by konzo View Post
One more question: How can I redirect all invalid addresses like http://kdelchev.com/?cat=7 to http://kdelchev.com/ i.e. to match everything that begin with ? after the /
That's exactly what the RewriteCond in my previous post does.

Regards
 
Old 12-14-2010, 02:59 AM   #6
konzo
Member
 
Registered: Dec 2008
Location: Bulgaria
Distribution: Debian / Slackware / Ubuntu
Posts: 230

Original Poster
Rep: Reputation: 34
Thanks for the reply, bathory

It work for ?cat=7 but didn't work for ?cat= and other invalid addresses.

I added the rule and the condition exactly, but the ?cat string stay at the end of the address and no redirect is made.
The rule is at the beggigning of the .htaccess file. I added [L] just in case the condition is overridden by another rule, but that didn't help.
I also try toescaspe the questiuon mark:

[code]RewriteCond %{QUERY_STRING} ^(.*)=
RewriteRule ^(.*)$ /\? [R=301][L][code]

Maybe there is some other options I miss...

Anyway, that works, but it will be better to be able to redirect all invalid addresses.

Last edited by konzo; 12-14-2010 at 03:02 AM.
 
Old 12-14-2010, 03:05 AM   #7
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
I guess you prepend the "RewriteEngine On" directive.
Also make sure that .htaccess files are read by apache. This is done by
Code:
AllowOverride All
in apache configuration file.
 
Old 12-14-2010, 03:31 AM   #8
konzo
Member
 
Registered: Dec 2008
Location: Bulgaria
Distribution: Debian / Slackware / Ubuntu
Posts: 230

Original Poster
Rep: Reputation: 34
Yes, I use RewriteEngine on. The htaacess file is read by apache. All changes I made make differencies.

Let's stay that way for now. Thank you very much fot the help!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache Rewrite mahmoud Linux - Software 2 11-11-2008 07:52 AM
Apache Rewrite mahmoud Linux - Newbie 2 10-08-2008 08:11 AM
apache rewrite help deesto Linux - Server 3 11-03-2006 02:07 PM
Need help with Apache rewrite kinetik Linux - General 6 05-19-2006 06:02 AM
Apache Rewrite Not Working mikeyt_333 Linux - General 1 05-20-2003 05:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 09:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration