Problem with RewriteCond %{QUERY_STRING}, backreference not dispaying in final URL - mod-rewrite

I have the following in my .htaccess file:
RewriteCond %{QUERY_STRING} ^route\=product\/category\&path\=35\&page\=([0-9]+)$
RewriteRule ^index\.php$ http://%{HTTP_HOST}/product/category/35/page_$1? [R=301,L]
It's not behaving as expected though, when I enter the URL:
http://example.com/index.php?route=product/category&path=35&page=2
It gets rewritten to:
http://example.com/product/category/35/page_
Could someone tell me what I have done wrong please?
Thanks,
eb_dev

To reference submatches of a RewriteCond directive, you need to use %n instead of $n:
RewriteCond %{QUERY_STRING} ^route=product/category&path=35&page=([0-9]+)$
RewriteRule ^index\.php$ /product/category/35/page_%1? [R=301,L]

Related

RewriteCond %{REQUEST_URI} not working

I am trying to redirect all requests coming in to the web server as http://portal.company.com/legacy to http://portal.company.com/wps/portal/public/legacy/legacyportlet with the following rule, but it is not working as expected.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^portal\.company\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/legacy$ [NC]
RewriteRule ^(.*)$ /wps/portal/public/legacy/legacyportlet$1 [NC,L,PT]
I have also tried
RewriteCond %{HTTP_HOST} ^portal\.company\.com$ [NC]
RewriteRule ^/legacy /wps/portal/public/legacy/legacyportlet [NC,L,PT]
Any help would be greatly appreciated!
Thanks
It doesn't look like your source or target URLs change in any way, so possibly you're better off using Apache's basic Redirect directive which just redirects one URL to another.
Use this rule:
RewriteCond %{HTTP_HOST} ^portal\.company\.com$ [NC]
RewriteRule ^legacy/?$ /wps/portal/public/legacy/legacyportlet [NC,L]
Remember that in .htaccess RewriteRule doesn't match leading slash of URI.

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.

removing the forward slash from this re-written url

I know this may sound silly little bit, but there is no other way to resolve it. I have an issue with the forward slash /. I want to remove it from the url because it is kind of making some confusion to the browsers giving trusted or un-trusted urls.
This is the code for the main domain:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^ /user [R=301,L]
and this is the output of the above code: https://www.mydomain.com/user/
but I want the url redirect to this:
https://www.mydomain.com/user
I also want to remove the forward slash from this url :
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(?:\.\w+|/)$
RewriteRule (.*) /$1/ [R,L]
RewriteCond %{REQUEST_URI} ^(/end)
RewriteRule ^(.*)$ /top/right/left/bottom/$1 [L]
RewriteCond %{REQUEST_URI} ^/top/right/left/bottom
RewriteRule top/right/left/bottom/(end)(/(./*))?$ $1$2 [R,L]
and this is the output of the above code: https://www.mydomain.com/end/
but I want the url redirect to this:
https://www.mydomain.com/end
Last question: why do I have to write www. in my own in some browser like FF or chrome so the redirect make itself to the required url?
All comments are appreciated
Looks like the above rewrites rules are written by me. So optimizing it for URL to not end with a / (https://www.mydomain.com/end)
Replace the whole(from RewriteCond %{ENV:REDIRECT_STATUS} 200 to RewriteRule top/right/left/bottom/(end)(/(./*))?$ $1$2 [R,L]) thing by this:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteRule ^(end)(?:/(.*))? /top/right/left/bottom/$1/$2 [L]
RewriteRule ^top/right/left/bottom/(end)(/.*)?$ $1$2 [R,L]
Remember though if you access with a forward slash like this: https://www.mydomain.com/end/ the forward slash will not be removed.
If you want the / to be removed: Replace the last 2 rules by these:
RewriteRule ^(end)(?:/(.+))? /top/right/left/bottom/$1/$2 [L]
RewriteRule ^(end)/$ $1 [R,L]
RewriteRule ^top/right/left/bottom/(end)(/.*)?$ $1$2 [R,L]
Store the url in a variable and perform rtrim to remove trailing slash
$redirectURL = rtrim($redirectURL, "/");

Apache mod_rewrite friendly URLs with corresponding 301 redirects

The Problem:
Been spinning my wheels and reading up on this one for awhile and looking for some help now. I'm looking to take a group of non-friendly URLs (there are actually more "groups" but this should me for an example):
domainname.com/?section=zebras
domainname.com/?section=monkeys&id=555
and turn them into friendly URLs, as well as do a 301 on the old versions, so that any old bookmarks (and search engines) will still resolve them. The new format I'm looking for would be:
domainname.com/zebras/
domainname.com/monkeys/555
I'm fully intending to write separate RewriteCond/RewriteRule combinations for each of those scenarios, so I don't necessarily need a super-rule that catches all my scenarios. Oh and this is all in .htaccess.
My Progress:
I was originally getting into a redirect loop because I was just doing two RewriteRules back to back - one for the friendly URL and one for the 301 redirect. Came across my favorite way (so far) around the redirect loop which works (for my scenario #1 at least):
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^section=zebras$ [NC]
RewriteRule ^.*$ http://www.domainname.com/zebras/? [R=301,NC,L]
RewriteRule ^zebras/$ /index\.php?section=zebras [NC,L]
However, I'd like to have something that works for more than just "zebras" (for instance, I'd like it to work for "lions" as well), so I'm trying to make that more generic. What I am trying now looks like this:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^section=([a-z]+)$ [NC]
RewriteRule ^section=([a-z]+)$ http://www.domainname.com/$1/? [R=301,NC,L]
RewriteRule ^([a-z]+)/$ /index\.php?section=$1 [NC,L]
However, this doesn't work. I think I have something "not quite right", I just can't tell what it is - there's something I'm missing or formatting incorrectly somewhere. Sorry in advance for the lengthy description, just wanted to be clear.
Do this:
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.\w+|/)$
RewriteRule (.*) /$1/ [R,L]
RewriteCond %{QUERY_STRING} ^section=([a-z]+)$ [NC]
RewriteRule ^ /%1/? [R=301,NC,L]
RewriteRule ^([a-z]+)/$ /index\.php?section=$1 [NC,L]
RewriteCond %{QUERY_STRING} ^section=([a-z]+)&id=(\d+)$ [NC]
RewriteRule ^ /%1/%2/? [R=302,NC,L]
RewriteRule ^([a-z]+)/(\d+)/$ /index\.php?section=$1&id=$2 [NC,L]
Description
Prevents looping:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
Prevents trailing slash problem:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.\w+|/)$
RewriteRule (.*) /$1/ [R,L]
Handles rewrites with only section=([a-z]+) in them:
RewriteCond %{QUERY_STRING} ^section=([a-z]+)$ [NC]
RewriteRule ^ /%1/? [R=302,NC,L]
RewriteRule ^([a-z]+)/$ /index\.php?section=$1 [NC,L]
Handles rewrites with only section=([a-z]+)&id=(\d+) in them:
RewriteCond %{QUERY_STRING} ^section=([a-z]+)&id=(\d+)$ [NC]
RewriteRule ^ /%1/%2/? [R=302,NC,L]
RewriteRule ^([a-z]+)/(\d+)/$ /index\.php?section=$1&id=$2 [NC,L]
mistake in your rules:
section=([a-z]+) is not available in the URI part. So, RewriteRule ^section=([a-z]+)$ never matched.

apache rewrite don't redirect?

apache .htaccess
RewriteRule ^promotion\.php\?do=content&id=38&mail$ promotion\.php?lang=tc&do=content&id=38 [R,L]
Thank you.
To match the query string, you have to use %{QUERY_STRING} like shown below.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^do=content&id=38&mail$ [NC]
RewriteRule ^promotion\.php$ promotion.php?lang=tc&do=content&id=38 [R,L]
Put it in the folder where promotion.php is present. I'm not sure how to involve RewriteCond ${HTTP_HOST} above.

Resources