Here is my htaccess code:
RewriteCond %{THE_REQUEST} ^(GET)\/site\/oldpage\.asp/\?id=(.*)&tid=(.*)\ HTTP
RewriteRule ^/newfolder/main/newpage-%2/#post-%1? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET)\/site\/oldpage\.asp/\?id=(.*)\ HTTP
RewriteRule ^/newfolder/main/newpage-%1/? [L,R=301]
`http://olddomain.com/site/oldpage.asp?id=32`
`http://olddomain.com/site/oldpage?id=677&tid=32`
The result works only on first parameter and redirect me to
My goal is to get this Urls:
if only id then
http://olddomain.com/main/newpage-32/
if id and tid then
http://olddomain.com/main/newpage-32/#post-677
without any querystring
what I getting now is :
both id and tid the results are
http://olddomain.com/main/newpage-677/?id=677&tid=32
if i send only id I get
http://olddomain.com/main/newpage-32/?id=32 works but with the query string at the end
Any idea how to solve it?
Related
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.
I'm trying to rewrite /user/username to /user-profile/?user=username without changing the URL in the address bar. I have the following code:
RewriteRule ^/?user/(.*?)/?$ /user-profile/?user=$1 [L]
RewriteCond %{THE_REQUEST} ^/user-profile/\?user=([^\&\ ]+)
RewriteRule ^/?user-profile/$ /user/%1? [L,R=301]
This changes /user/username to /user-profile/?user= in the address bar. The query string var "user" is left blank but it somehow loads the correct user profile. So I think the first rule is working but the second rule and it's condition must not be since the URL in the address bar is changing. What can I do to fix it?
Thanks!
A couple of things. You have 2 rules, one that internally rewrites and the other redirects the browser. You must have your redirect come before the rewrite, otherwise the internal rewrite gets reprocessed by the redirect rule. The other thing is the %{THE_REQUEST} variable is literally the first line of the HTTP request, and it starts with a method, not the request URI:
GET /user-profile/?user=qwerty HTTP/1.1
is what it will look something like. That means you need to alter your regex to account for those other parts of the request that's not the URI. Try:
RewriteCond %{THE_REQUEST} ^GET\ /user-profile/\?user=([^\&\ ]+) [NC]
RewriteRule ^ /user/%1? [L,R=301]
RewriteRule ^/?user/(.*?)/?$ /user-profile/?user=$1 [L]
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!)
I would like to redirect without looking at the query string, and my redirect result no need append the query string as well, so I add a ? at the end of the RewriteRule.
I tried the following syntax, but the outcome just close to it.
RewriteCond %{QUERY_STRING} .* [NC]
RewriteRule ^exd\.asp$ http://www.example.com/index.php?r=p/consumer? [R=301,L]
and also, i tried to escape the first ?, which I need it, but still the same outcome.
RewriteRule ^exd\.asp$ http://www.example.com/index.php\?r=p/consumer? [R=301,L]
Outcome:
http://www.example.com/index.php?r=p/consumer%3f
I want to get ride of the %3f.
Thanks!
You don't need to append a ? at the end if you already have a query string in your target. Just do this:
RewriteCond %{QUERY_STRING} .* [NC]
RewriteRule ^exd\.asp$ http://www.example.com/index.php?r=p/consumer [R=301,L]
By default, query strings get appended, like this:
RewriteRule ^foo$ /bar [L]
You request /foo?blah and you get /bar?blah
However, if you have a ? in your target, query strings won't get appended unless you have the QSA, so:
RewriteRule ^foo1$ /bar? [L]
RewriteRule ^foo2$ /bar?q=2 [L]
You request /foo1?blah and you get /bar, you request /foo2?blah and you get /bar?q=2. If you include a QSA in the rewrite flags, then &blah gets appended to the end.
i want an html redirection to a different php and params, the phps are inside a subdirectory , for example, from an /subdir/index.php?thread=23-post=12 /subdir/to showthread.php?topic=23&topic=12. But isn't working this:
RewriteRule ^/test/index\.php?thread=(.*)-post=(.*)$ /test/showthread.php?topic=$1&topic=$2 [R]
Any suggestion?
thanks in advance
You can't match against the query string in a RewriteRule. You need to match against the %{QUERY_STRING} var in a RewriteCond and use the % backreference:
RewriteCond %{QUERY_STRING} ^thread=([^-]+)-post=(.*)$
RewriteRule ^/?test/index\.php$ /test/showthread.php?topic=%1&topic=%2 [L,R]
Note that your rule maps 2 things to the topic query string param.