htaccess mod rewrite changes http://www to http:/www - .htaccess

I want to replace calls like this:
www.mysite.com/sub/file.php?param1=x&param2=http://www.someurl.com
with:
www.mysite.com/sub/param1/param2
Param 1 is an integer number Param 2 is a url
I wrote this rewrite rule in htaccess:
RewriteCond %{REQUEST_URI} \/sub\/
RewriteRule sub\/([0-9]+)\/(.*)$ sub\/file.php?param1=$2&param2=$1 [L]
Unfortunately param2 (the URL) starts with http:/www.someurl.com instead of http://www.someurl.com (note the single slash).
Any idea what causes it? When I call the same file with same parameters in the format www.mysite.com/sub/file.php?param1=x&param2=http://www.someurl.com , param2 does appear OK so it must be something with the rewrite rule.

You need to grab the value from THE_REQUEST:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sub/[0-9]+/([^?\ ]+)
RewriteRule ^sub/([0-9]+)/ sub/file.php?param1=$1&param2=%1 [L]

Related

add string at the end of URL using htaccess which has query string also

I want to change
domain.com/division1/index.php?members/maxmusterman.5
to
domain.com/division1/index.php?members/maxmusterman.5/#div
That is if the URL contains index.php?members, then I add /#div at the end of url. I tried this
RewriteEngine On
RewriteCond %{REQUEST_URI} index.php?
RewriteRule (.*) /%1/%{QUERY_STRING}&123 [L,QSA,R=301]
but it returns
domain.com/members/maxmusterman.5&123?members/maxmusterman.5
Note here that &123 is attached after URI before starting parameters. I researched htaccess QSA flag but I could not find a way to add a custom string at the end of the query string. How can I do that. Here I have used &123 for test purpose, actual requirement is adding /#div
To redirect
domain.com/division1/index.php?members/maxmusterman.5
to
domain.com/division1/index.php?members/maxmusterman.5/#div
.
You can use something like the following :
RewriteEngine on
RewriteCond %{QUERY_STRING} !loop=no
RewriteRule ^division1/index\.php$ %{REQUEST_URI}?%{QUERY_STRING}&loop=no#div [L,R,NE]
I added an additional perameter loop=no to the destination url to prevent infinite loop error .You can't avoid this as both your old url and the new url are identical and can cause redirect loop if you remove the RewriteCond and Query perameter.
NE (no escape ) flag is important whenever you are redirecting to a fragment otherwise mod-rewrite converts the # to its hex %23 .
solution #2
RewriteEngine on
RewriteCond %{THE_REQUEST} !.*loop=no [NC]
RewriteCond %{THE_REQUEST} /division1/index\.php\?(.+)\s [NC]
RewriteRule ^ /division1/index.php?%1&loop=no#div [NE,L,R]
Clear your browser cache before testing these redirects.

301 Redirects with mod_rewrite

I'm working with mod_rewrite under .htaccess, and I'm trying to redirect (R=301) an URL like this :
http://domain/index.php?folder=AB_CD
to an URL like this
http://domain/AB/CD/
How can I write the rule please ?
Try the following code in root/.htaccess :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC]
RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R]
Explaination :
RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC]
Checks to ensure that the url (index.php) has query strings with specific key and value, ( folder=foo_bar) acording to the regex pattern, if the url has valid query strings then the rule is processed
RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R]
index.php?query_strings gets redirected to /query/strings, if the condition is met.
Empty question mark ? at the end of the Rewrite target is important as it discards the orignal query strings, without it /index.php?folder=foo_bar redirects to /foo/bar/?folder=foo_bar appending the old query strings.
(Hope, this helps!)

mod_rewrite and redirect causing loop

I have problem when I try to redirect and rewrite together.
I have site example.com/show_table.php?table=12 (max 99 tables). I wanted nice links, so I got this .htacces rw rule:
RewriteRule ^table/([0-9]{1,2})$ show_table.php?table=$1 [L,NC]
Now are links something like example.com/table/12 - it's definitely OK. But I want all old links redirect to new format. So I use Redirect 301, I added to .htaccess this code:
RewriteCond %{REQUEST_URI} show_table.php
RewriteCond %{QUERY_STRING} ^table=([0-9]{1,2})$
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
But when I visit example.com/show_table.php?table=12, I receive just redir-loop. I don't understant - the first is rewrite, the second is redirection, there ain't no two redirections. Do You see any error?
Thanks!
Instead of checking REQUEST_URI in the condition, you need to be checking in THE_REQUEST (which contains the full original HTTP request, like GET /show_table.php HTTP/1.1). When Apache performs the rewrite, it changes REQUEST_URI, so to the rewritten value, and that sends you into a loop.
# Match show_table.php in the input request
RewriteCond %{THE_REQUEST} /show_table\.php
RewriteCond %{QUERY_STRING} ^table=([0-9]{1,2})$
# Do a full redirection to the new URL
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
# Then apply the internal rewrite as you already have working
RewriteRule ^table/([0-9]{1,2})$ show_table.php?table=$1 [L,NC]
You could get more specific in the %{THE_REQUEST} condition, but it should be sufficient and not harmful to use show_table\.php as the expression.
You'll want to read over the notes on THE_REQUEST over at Apache's RewriteCond documentation.
Note: Technically, you can capture the query string in the same RewriteCond and reduce it to just one condition. This is a little shorter:
# THE_REQUEST will include the query string so you can get it here.
RewriteCond %{THE_REQUEST} /show_table\.php\?table=([0-9]{1,2})
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]

htaccess rewrite querystring and remove empty value

first, sorry for my bad English.
I try to rewrite url generated from Form Get and redirect that.
my url is like this:
http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city
and I have this .htaccess configured:
11. RewriteCond %{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]
So basically all my request are direct to index.php.
21. RewriteCond %{REQUEST_URI} !index\.php|resources|hidden
22. RewriteRule ^(.*)$ index.php/$1 [L]
All works, but the problem is when I have an empty value in query string, the rule add double slash and the above url (for example whit &type=&zone=my-zone... type have empty value) will translate like that:
http://www.mysite.com/for-rent/my-category//my-zone/my-city/
The question is: How can i remove in .htaccess the double slash generated if i have one or more empty value in query string?
Thanks
Easiest is to do another redirect (not real pretty as it requires two 301's).
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R=301,L]
The fun part is that when the url is loaded with a double slash in it, mod_rewrite will automatically remove this. So as you can see above you'll just have to rewrite the url to itself, kind of.

Why won't my RewriteRules play nice together?

The first part of my code works and redirects/rewrites to new/file. Problem occurs when I try to pass variables. They all get redirected to new/file
Whatever variable I try to pass in file.php?foo=bar redirects to new/file instead of new/file/bar.
for example:
new/file rewrites to file.php
new/file/2 rewrites to file.php?page=2
file.php redirects to new/file
file.php?page=2 doesn't redirect to new/file/2 but get's overwritten to new/file instead
My code:
RewriteBase /domain.com
#regular
RewriteCond %{QUERY_STRING} !redirect=no
RewriteRule ^file\.php$ new/file? [NS,R=301,L]
RewriteRule ^new/file?$ file.php?redirect=no [NS]
#with variable
RewriteCond %{QUERY_STRING} ^page=([0-9-]+)/?$
RewriteRule ^file\.php$ new/file/%1? [NS,R=301,L]
RewriteRule ^new/file/([0-9-]+)/?$ file.php?page=$1&redirect=no [NS]
Please note that I am passing more than just page variables.
I'm thinking maybe a QSA flag is supposed to go somewhere?
RewriteCond %{QUERY_STRING} !redirect=no
RewriteRule ^file\.php$ new/file [NS,R=301,L]
RewriteRule ^new/file$ file.php?redirect=no [NS]
In line 2 you had ?. This will replace the current querystring, and page=2 is lost.
In line 3 you had a ?. Paths don't contain questionmarks. Secondly the questionmark is interpreted as an "optional letter 'e'", because it is a RegEx. So "/new/fil" would also work (try it out).

Resources