LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Apache rewrite regex (https://www.linuxquestions.org/questions/linux-server-73/apache-rewrite-regex-849999/)

konzo 12-13-2010 04:17 PM

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

bathory 12-13-2010 05:58 PM

Hi,

You can use the following:
Code:

RewriteEngine on

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

Regards

kbp 12-13-2010 06:21 PM

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

konzo 12-14-2010 02:34 AM

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 /

bathory 12-14-2010 02:52 AM

Quote:

Originally Posted by konzo (Post 4190707)
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

konzo 12-14-2010 02:59 AM

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.

bathory 12-14-2010 03:05 AM

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.

konzo 12-14-2010 03:31 AM

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!


All times are GMT -5. The time now is 04:22 AM.