I try to write a RewriteRule in the .htaccess but I have problems
I try to redirect from:
blog/entrada.php?id=2
To:
/blog/3D-touch
This is one of the multiple things I tried and does not work:
RewriteRule ^blog\/entrada\.php\?id=2$ /blog/3D-touch [L,R=301]
What is wrong with my Rule. How to redirect effectively?
Thanks
Querystring is not part of match in RewriteRule directive, to redirect query strings, you need to use RewriteCond one of the following options :
option 1
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/entrada\.php\?id=2 [NC]
RewriteRule ^ /blog/3D-touch? [NC,L,R]
option 2
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=2$ [NC]
RewriteRule ^blog/entrada\.php$ /blog/3D-touch? [NC,L,R]
We use an empty question mark ? at the end of the target url to discard the old query strings, otherwise these query strings get appened to the target url by default.
Change the R to R=301 if you want to make the redirection permanent.
use from this code
RewriteRule blog/(.*) blog/entrada.php?id=$1
this code will redirect all urls which have blog/ to blog/entrada.php and put after value of blog/ to $_GET['id']
you should have following code n the top location of your htaccess file
Options +FollowSymLinks
RewriteEngine on
Related
I need using this .htaccess file. It only changes the URL from www.mydomain.com/users/user?usernam=myname123&profile=myprofile123&data=mydata123 to www.example.com/users/myname123/myprofile123/mydata123 .
i use
RewriteEngine On
RewriteRule ^user\.php$ - [L]
RewriteRule ^([0-9a-zA-Z\-_.]*)/?$ /users/user.php?username=$1&profile=$2&data=$3[L,QSA]
htaccess in users folder
not work
With your shown samples, could you please try following rules. This will take anything in REQUEST_URI starting.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{THE_REQUEST} /([^/]*)/([^/]*)/([^/]*)/([^/]*)/?\s
RewriteRule ^(.*) /%1?username=%2&profile=%3&data=%4 [NE,L,NC]
OR(either use above or following one) in case you want to match users in URI then run rules then try following.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{THE_REQUEST} /users/([^/]*)/([^/]*)/([^/]*)/?\s
RewriteRule ^(.*) /users?username=%1&profile=%2&data=%3 [NE,L,NC]
Explanation: Adding detailed explanation for above.
RewriteEngine ON: Enabling RewriteEngine here to enable mod rule writing.
RewriteCond %{QUERY_STRING} ^$: Checking condition if query string is NULL then go further in rules.
RewriteCond %{THE_REQUEST} /users/([^/]*)/([^/]*)/([^/]*)/?\s: Matching regex in THE_REQUEST from /users to till spaces and capturing 3 groups values into back references to be used later.
RewriteRule ^(.*) /users?username=%1&profile=%2&data=%3 [NE,L,NC]: Using url rewrite to change URI to as per OP's request which has back references values in it.
I want to change the URLs on my website from page.php?id=1&name=john to page/1/john using htaccess RewriteRule.
This is what I have currently but it is not working as expected:
RewriteRule ^page/([0-9]+)/([a-zA-Z\s-]+) page.php?id=$1&name=$2
Is it possible to make this change in htaccess rules or should I change every link to <a href='page.php/1/john'>Page</a> which is tiresome since I have got many links in every page. Help is appreciated. Thanks.
You may use these 2 rules in site root .htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /rootfolder/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /page\.php\?id=(\d+)&name=([^\s&]+)\s [NC]
RewriteRule ^ page/%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^page/(\d+)/([^/]+)/?$ page.php?id=$1&name=$2 [L,QSA,NC]
1st solution: To get from query string URL to user friendly url try following, as per OP's request.
Options -MultiViews
RewriteEngine On
RewriteBase /rootfolder/
RewriteCond %{QUERY_STRING} ^id=(\d+)&name=([\w-]+)/?$ [NC]
RewriteRule ^([^.]*)\..*$ /$1/%1/%2/ [QSD,R=302,NC,L]
2nd solution: As far as I get from OP's question, could you please try following. Considering as per thumb rule users will be given friendly URL like eg--> http://localhost:80/page/1/john and it will point in pointed to http://localhost/page.php?id=1&name=john in backend.
Options -MultiViews
RewriteEngine On
RewriteBase /rootfolder/
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ /$1.php?id=$2&name=$3 [NC,L]
I have a redirect problem where I would like to redirect a url with query parameters to another PDF entirely, e.g.:
Original URL:
https://www.foo.bar.com/~/bar.pdf?la
Redirect URL:
https://www.bar.foo.com/foo.pdf
Currently my rewrite rule is as shown below:
RewriteRule ^/~/bar.pdf?la https://www.bar.foo.com/foo.pdf [R=301,L,NC]
How can I write a 301 redirect that both accounts for the tilde and the query parameter?
Thanks in advance for your help!
First your rule RewriteRule ^/~/bar.pdf?la https://www.bar.foo.com/foo.pdf [R=301,L,NC] has several issues .
First , RewriteRule ^/ it is not applicable here so RewriteRule ^ is enough.
Second , you should escape ~ like this RewriteRule ^\~
and finally , dont't write query string with RewriteRule like bar.pdf?la because it is not a pert of URI.
The line should look like this :
RewriteRule ^\~/bar.pdf https://www.bar.foo.com/foo.pdf [R=301,L,NC]
But the query string may append to new target , to prevent that add ? at the end of target this :
RewriteRule ^\~/bar.pdf https://www.bar.foo.com/foo.pdf? [R=301,L,NC]
If you want to use query string value , you may add condition before that to utilize it:
RewriteCond %{QUERY_STRING} ^(.*)$
you could change ^(.*)$ to whatever you want and you could use it in target like this example :
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ https://www.whatever.com/%1/$1 [R=301,L,NC]
So , %1 refer to this condition RewriteCond %{QUERY_STRING} ^(.*)$ and $1 refer to this condition RewriteRule ^(.*)$ and so on.
You need to use RewriteCond
directive to check the url query string. You can not do that in RewriteRule . The querystring in your url is la .
RewriteEngine on
RewriteCond ℅{QUERY_STRING} ^la$ [NC]
RewriteRule ^.*bar\.pdf$ /foo.pdf? [L,R=301]
I have a Forum on my site and need to redirect weirdly generated urls.
Every url contains ?id=, eg:
https://www.example.com/forum/topic/casualthread/page/25?id=casualthread
and I need to remove the ?id= and everything that follows in order to have:
https://www.example.com/forum/topic/casualthread/page/25
I am trying to modify this code I found here on Stackoverflow with very scarce results:
RewriteEngine On
RewriteBase /
# Make sure there is a query string
RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /?id= [R=301,L]
The htaccess file i am editing is in the forum directory:
https://www.example.com/forum/
and it redirects everything to the homepage https://www.example.com: what am I doing wrong?
You can use this
RewriteEngine on
RewriteCond %{QUERY_STRING} id=
RewriteRule ^ %{REQUEST_URI}? [L,R]
Since not just id, but also some other parameter(s) may be present in the query string, use:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*)(^id=[^&]*&?|&id=[^&]*)(.*)$
RewriteRule ^(.+) /$1?%1%3 [R=301,L]
my current regex code to beautify a url (www.abc.com/user.php?url="rio") in .htaccess file is
RewriteCond %{REQUEST_URI} user
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)$ user.php?url=$2
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/$ user.php?url=$2
It works fine and the page url is beautified to :
www.abc.com/user/rio
but there is a problem now, Now even if I change url like in following ways:
www.abc.com/user/user/user/rio
www.abc.com/user///rio
www.abc.com/user/abc/rio
These links work same way, and that is making content duplication.
Please help!
You may try these rules in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# remove multiple slashes
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ /$0 [R=301,L,NE]
# handles /user/something
RewriteRule ^user/([\w-]+)/?$ user.php?url=$1 [L,QSA,NC]
Options -MultiViews is required to turn off content negotiation feature.