htaccess redirect query string, but encoded ? to & - apache

I want to redirect
https://www.aaa.com/query?v=abc123_ABC
to:
https://bbb.com/?url=https://www.aaa.com/query?v=abc123_ABC
I use this in .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://bbb.com/?url=https://www.aaa.com/$1 [L,B,NE,NC,QSA,R=301]
</IfModule>
But the output is:
https://bbb.com/?url=https://www.aaa.com/query&v=abc123_ABC
The ? was converted into &, what's wrong with the code?
Thanks.
====
You can test it here:
https://htaccess.madewithlove.be/

As per OP's request and shown samples could you please try following.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s(/query)\?(v=abc123_ABC)\s [NC]
RewriteRule ^(.*)$ https://bbb.com/?url=https://%{HTTP_HOST}%1?%2 [NE,L]

The following rule should work.
RewriteEngine on
RewriteCond %{THE_REQUEST} /query\?v=.+\s [NC]
RewriteRule ^ https://bbb.com/?url=%{REQUEST_SCHEME}://%{HTTP_HOST}%{REQUEST_URI}?%{QUERY_STRING} [L,R]

Related

How rewrite url in htaccess

Good Morning,
I have documents within the structure of my website, which are accessed with the following url format:
http://example.com/docs/files/123/mydoc.pdf
http://example.com/docs/files/475/otherdoc.pdf
I want when a url of the above type is accessed, it is renamed with the following format:
http://example.com/123/mydoc
http://example.com/475/otherdoc
I have the following rules in the htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^/docs/files/([0-9]+)/([a-zA-Z0-9-]+)\.([a-zA-Z-]+) /$1/$2 [QSA,L]
</IfModule>
If I type in the browser
http://example.com/docs/files/123/mydoc.pdf
It doesn't change the url to
http://example.com/123/mydoc
What am I doing wrong?
Thanks
You may try these rules in your site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+files/(\S+)\.pdf[?\s] [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/files/$1.pdf -f
RewriteRule ^(.+?)/?$ files/$1.pdf [L]
With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s(/files/.*)\.pdf\s [NC]
RewriteRule ^ http://%{HTTP_HOST}%1 [R=301,NE]
RewriteRule ^(.*)/?$ files/$1.pdf [L]

Issue redirecting query string to path

I want to redirect a get query parameter to a path and maintain the ability to use the GET parameter in the code.
I struggle to find examples excluding the file extension so perhaps this is what is throwing me.
I have tried the below code but not getting expected results from it?
RewriteCond %{REQUEST_URI} ^/spares/$
RewriteCond %{QUERY_STRING} pid=([0-9]*)$
RewriteRule ^(.*)$ /spares/%1 [R=302,L]
For example:
mysite.com/spares/?pid=x
to
mysite.com/spares/x
EDIT:
Copy of .htaccess in /dev/ subdirectory.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /dev/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /dev/index.php [L]
</IfModule>
RewriteEngine On
# To externally redirect with pid
RewriteCond %{THE_REQUEST} \s+spares/\?pid=(.*)\s [NC]
RewriteRule ^ /spares/%1 [R=301,L,NE]
# To internally rewrite to ?pid=
RewriteCond %{QUERY_STRING} pid=(.+)$
RewriteRule ^dev/spares/$ /dev/spares/?pid=%1 [NC,L]
# END WordPress
Try with:
RewriteEngine On
# To externally redirect with pid
RewriteCond %{THE_REQUEST} \s/+spares/\?pid=(.*)\s [NC]
RewriteRule ^ /spares/%1 [R=301,L,NE]
# To internally rewrite to ?pid=
RewriteCond %{QUERY_STRING} pid=(.+)$
RewriteRule ^spares/$ /spares/?pid=%1 [NC,L]

.htaccess redirect with argument

I tried out several redirects but none worked for my needs. Could you please help me to figure out this one? I know, it has been answered many times but I can not get it to work. Thanks.
I need to redirect from to
Examples:
/demo/details?acronym=text15
to
/demo/text15
or
/demo/details?acronym=text-c15
to
/demo/text-c15
The correct redirect was:
#redirect from "demo/details\?acronym=some-string" to "/demo/some-string"
RewriteCond %{THE_REQUEST} /details\?acronym=([^\s]+) [NC]
RewriteRule ^ /demo/%1? [NC,L,R]
it is very simple
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^demo/([a-z0-9-]+)$ /demo/details?acronym=$1 [QSA,NC,L]
and the most important part here is that you should call the formatted link not the original link
e.g. /demo/text15
thats all folks
You can use the following rule in your .htaccess:
RewriteEngine on
#redirect from "demo/details/acronym\?text=foo" to "/demo/foo"
RewriteCond %{THE_REQUEST} /details/acronym\?text=([^\s]+) [NC]
RewriteRule ^ /demo/%1? [NC,L,R]
#skip the rule if the request is for dir
RewriteCond %{REQUEST_FILENAME} !-d
#skip the rule if the request is for file
RewriteCond %{REQUEST_FILENAME} !-f
#rewrite any other request to "/demo/details?acronym="
RewriteRule ^(?:demo/)?([^/]+)/?$ /demo/details/acronym?text=$1 [NC,L]

Change page url in htaccess

I have my own CMS system, but I can't make a code in .htaccess file.
I want redirect from http://example.com/?page=forum to http://example.com/forum...
I don't know, how do it...
I tried the following:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /projects/cms/
RewriteCond ?page=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
But it didn't work.
Sorry for my bad English, thanks for Code.
The comment splash58 posted is correct - you need to check the QUERY_STRING for the presence of page=<something>, like this:
RewriteCond %{QUERY_STRING} page=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]
Your file should now look like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /projects/cms/
RewriteCond %{QUERY_STRING} page=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
If it is still not working, then you need to ensure that mod_rewrite is enabled on your server, and that AllowOverride All is set in your server or virtual host configuration. See this for more information on how to do so: http://tildemark.com/enable-htaccess-on-apache/

RewriteRule for .htaccess

I have this rule in .htaccess:
RewriteRule ^([^/]*)$ /user.php?username=$1 [L]
Output:
http://domain.tld/username
Desired output:
http://domain.tld/user/username
How can I do this?
Here is how to change a URL with a query string to a path:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/user.php [NC]
RewriteCond %{QUERY_STRING} ^.*username=([a-zA-Z]+).*$ [NC]
RewriteRule ^(.*)$ /user/%1? [L]
Input
http://domain.tld/user.php?username=bob
Output
http://domain.tld/user/bob
Here is how to go the other way:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/user/ [NC]
RewriteRule ^user/(.+)$ /user.php?username=$1 [L]
Input
http://domain.tld/user/bob
Output
http://domain.tld/user.php?username=bob
You can test .htaccess rules here: http://htaccess.madewithlove.be/
You can use this code in your DOCUMENT_ROOT/.htaccess file:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^user/([^/]+)/?$ user.php?username=$1 [L,NC,QSA]