help rewriting this url. simple but not working - mod-rewrite

RewriteCond %{REQUEST_URI} !^/?ping.php
RewriteRule ^/\?(.*)$ ping.php?url=$1 [L]
i am trying to match any character that follows /?
www.mysite.com/?someurl.com
instead it keeps loading the index.html !

You cannot match the query string with mod_rewrite, but if you still want to pass it on, you can add %{QUERY_STRING} to the resultant url, for example
RewriteRule ^.*$ ping.php?url=%{QUERY_STRING} [L]

Related

Relative path and mod_rewrite issue

Have a trouble with mod_rewrite.
I want :
htp://example.com/a/some_text -> htp://example.com/?p1=some_text and
htp://example.com/b/some_text -> htp://example.com/?p1=some_text
So, I type:
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^(.*)$ ?fsearch=$2 [QSA]
and get wrong relative paths in css, such as htp://example.com/a/CSS/main.css instead of htp://example.com/CSS/main.css. And get nothing in $2 too.
Help, please
$2 tries to pick the second capture group of the RewriteRules subject, but there is only one! You probably mean %2 which picks from the RewriteCond...
Since you only append a query string in your RewriteRule the original URI (path) is left untouched.
You can simplify the pattern in the RewriteRule since you are not interested in the subject anyway.
This is probably what you are looking for:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^ /?fsearch=%3 [QSA]
Much better however would be to rewrite directly to whatever script you actually want to process the request to safe another rewriting round to find the index document, so something like:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^ /index.php?fsearch=%3 [QSA]
And unless you decide to use the first capture group in the RewriteCond you can also simplify that further:
RewriteEngine on
RewriteCond %{REQUEST_URI} \/(a|b)\/(.*)$
RewriteRule ^ /index.php?fsearch=%2 [QSA]

mod_rewrite to redirect URL with query string

I have a lot of working mod_rewrite rules already in my httpd.conf file. I just recently found that Google had indexed one of my non-rewritten URLs with a query string in it:
http://example.com/?state=arizona
I would like to use mod_rewrite to do a 301 redirect to this URL:
http://example.com/arizona
The issue is that later on in my rewrite rules, that 2nd URL is being rewritten to pass query variables on to WordPress. It ends up getting rewritten to:
http://example.com/index.php?state=arizona
Which is the proper functionality. Everything I have tried so far has either not worked at all or put me in an endless rewrite loop. This is what I have right now, which is getting stuck in a loop:
RewriteCond %{QUERY_STRING} state=arizona [NC]
RewriteRule .* http://example.com/arizona [R=301,L]
#older rewrite rule that passes query string based on URL:
RewriteRule ^([A-Za-z-]+)$ index.php?state=$1 [L]
which gives me an endless rewrite loop and takes me to this URL:
http://example.com/arizona?state=arizona
I then tried this:
RewriteRule .* http://example.com/arizona? [R=301,L]
which got rid of the query string in the URL, but still creates a loop.
Ok, adding the 2nd RewriteCond finally fixed it - now rewriting correctly and no loop:
# redirect dynamic URL: ?state=arizona
RewriteCond %{QUERY_STRING} state=arizona [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule .* http://domain.com/arizona? [R=301,L]
# older rewrite rule that passes query string based on URL:
RewriteRule ^([A-Za-z-]+)$ index.php?state=$1 [L]
And here's the code to make it work for any state value, not just arizona:
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{QUERY_STRING} ^state=([A-Za-z-]+)$ [NC]
RewriteRule .* http://domain.com/%1? [R=301,L]
RewriteCond %{REQUEST_URI} ^/\?state=arizona$ [NC]
should be
RewriteCond %{QUERY_STRING} state=arizona [NC]
The request_uri ends at the ? token.

Rewrite URL on submit form

I have a search form and when I make a search I get this URL "http://****/video/view/search/?imeto_tam=tarsene" but I want to replace this the "?imeto_tam=tarsene" with the word I search for and my address to look like this - "http://****/video/view/search/tarsene". Generally I use mod_rewrite on my site and it's working for my links but it's not working for the form-s. Could someone tell me how to do it?
RewriteEngine On
RewriteRule ^view/([0-9a-zA-Z\-\(\)]+)/?$ index.php?a=$1 [L]
RewriteRule ^view/([0-9a-zA-Z\-():]+)/([0-9a-zA-Z\-():\.,]+)$ index.php?a=$1&id=$2 [L]
These rules would go into your root .htaccess file and 301 redirect the querystring imeto_tam to the folder and then the next rule would make it get passed to your code (index.php)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?imeto_tam=([^&]+)(&.*)?$ [NC]
RewriteRule ^video/view/search/(index\.php)?$ /video/view/search/%2/? [R=301,L]
RewriteRule ^video/view/search/([^/]+)/$ /video/view/search/index\.php?imeto_tam=$1 [L]

Replace ? in request with RewriteRule

site.com/link?p=2
give $_GET['p']==2 even though i've already made
site.com/link
rewrite to
site.com/index.php?page=link
So, i'm trying to replace site.com/link?p=2
with site.com/link&p=2
RewriteEngine on
RewriteRule (.*)\?(.*) $1\&$2
RewriteCond %{REQUEST_URI} !\....$
RewriteRule ^(.*)$ /index.php?p=$1
It doesn't work!
RewriteRule cannot see query strings (the ? and anything after it) on the left-hand side; it matches only on the path part of the URL.
But the good news is, all you probably need to do is this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !\....$
RewriteRule ^(.*)$ /index.php?p=$1 [QSA]
The QSA option, Query String Add, tells your RewriteRule to add to the query string instead of replacing it (the default behavior, which doubtless prompted the whole issue).

How to forward one url to another using mod_rewrite rules

Im trying to forward this url, but its not working - here is my code:
RewriteRule ^https://www.wsjwine.com/discovery_offer.aspx?promo=2227006$ https://www.wsjwine.com/discovery_offer_lp2.aspx?promo=2227006 [L]
With RewriteRule directive you can only test the URL path. For further tests you need to use additional RewriteCond directives.
Now if you want to rewrite every request of /discovery_offer.aspx to /discovery_offer_lp2.aspx regardless of how the query looks like, you can just use this (example for the .htaccess file in the root directory):
RewriteRule ^discovery_offer\.aspx$ discovery_offer_lp2.aspx [L]
If you don’t specify a query in the substitution, the originally requested query is automatically appended to the new one.
And if you just want to rewrite that specific URL, try this:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} =www.wsjwine.com
RewriteCond %{QUERY_STRING} =promo=2227006
RewriteRule ^discovery_offer\.aspx$ discovery_offer_lp2.aspx [L]
You can't detect query strings like that. Use RewriteCond %{QUERY_STRING}.

Resources