.htaccess rewrite query string as path url - apache

I searched for this question but only came across very specific answers that I couldn't tailor to my requirements.
My URL now looks like this: https://example.eu/?action=changepassword and I want it to look like this: https://example.eu/changepassword so text ?action= gets deleted.
I tried to adapt this but it didn't work.

With your shown samples, please try following htaccess rules file. Please also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect rules here....
RewriteCond %{THE_REQUEST} \s/?\?action=(\S+)\s [NC]
RewriteRule ^ %1? [R=301,L]
##Internal rewrite rules to handle query string in backend.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /?action=$1 [L]

Related

Rewriting URLs with htaccess, multiple parameters

I'm trying to rewrite something like this:
https://mywebsite.com/pages/article.html?id=1&title=Title-Goes-Here
into
https://mywebsite.com/pages/article/1/Title-Goes-Here
Using this Rewrite Rule
RewriteEngine on
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+)$ article.html?id=$1&title=$2 [NC,L]
However, when I try this code in https://htaccess.madewithlove.com/ it gives me
This rule was not met.
Also tried it on my website htaccess file with no result. I don't know where is the problem.
Please don't create OR test rules on online sites, they are NOT trust worthy, so kindly test these rules into your localhost OR apache.
With your shown samples/attempts, please try following htaccess rules. Considering that you are hitting URL https://mywebsite.com/pages/article.html?id=1&title=Title-Goes-Here in browser AND you want to redirect it to URL https://mywebsite.com/pages/article/1/Title-Goes-Here in browser.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect in browser rules here....
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/([^/]*)/([^.]*)\.html\?id=([^&]*)&title=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L]
##Internal rewrite to html file rules here....
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ $1/$2.html?id=$3&title=$4 [QSA,NC,L]

How to I replace a part of the URL string containing a query using htaccess?

I have a URL string using a PHP query string.
I want to make the URL pretty, but I haven't been able to make the rule properly.
Currently the URL looks like this:
http://localhost/pages/map?name=Skyfall-2022
But I want it to look like this:
http://localhost/pages/Skyfall-2022
This is my current htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^pages/(.+)$ /pages/map?name=$1
I get a 500 Internal Error with this when I try to type in the desired URL. I don't really understand Regular Expressions so I would appreciate if someone could adjust this for me with an updated .htacces
With your shown samples, please try following htaccess rules.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect for url change in browser rules here...
RewriteCond %{THE_REQUEST} \s/(pages)/map/?\?name=(\S+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
##Internal rewrite here for internal file's serving here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ $1/all-maps.php?name=$2 [QSA,L]
NOTE: I have mentioned map? in rewrite rules(2nd set of rules written in comments also in rules), you can change it with your php file's name whatever php file you have to pass query string to.
NOTE2(OP's fixes as per OP environment): tweaked 1 rule a
bit to $1/all-maps.php?name=$2 to $1/map.php?name=$2 and moved the all-maps.php one directory down and these rules worked fine, mentioned by OP in comments here. Just sharing here, in future it could help people that apart from above rules this was done as part of solution.

Redirect www.example.com/some-path/example/ to www.example.com/some-path/?ABC=123

I'm trying to redirect a path like this: from www.example.com/some-path/sg/ to www.example.com/some-path/
But I need some way of identifying this traffic such a parameter, ideally: www.example.com/some-path/?ls=sg. Is this possible using htaccess/mod_rewrite?
What I have tried is:
RedirectMatch 301 /sg/(.*) /$1?ls=SG7
With your shown attempts, samples please try following htaccess Rules file.
Make sure to place them at the top of your htaccess rules file.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##To look for sg here.
RewriteCond %{THE_REQUEST} sg [NC]
RewriteRule ^(.*)/(.*)/?$ /$1/?ls=$2 [R=301,NE,L]

Rewrite urls friendly with htaccess. how I can use it on my site?

I'm rewriting urls on htaccess using rewrite rules, but when I want that my site uses it I get stuck.
I want my links looks like:
mysite.com/section/this-is-the-title-of-this-section
I used rewrite rules to get this:
mysite.com/section/?id=longalfanum to mysite.com/section/longalfanum
that was cool for a moment. but I checked some few websites for the URLs I realize that they have friendly URL from the beginning
so I changed the url on the to looks like:
mysite.com/section/longalfanum-the-title-of-the-section
now my links doesn't work like before.
my htaccess looks like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#this is for avoid extra /
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
RewriteCond %{QUERY_STRING} !^$
RewriteRule .*/mysite.com/%{REQUEST_URI}? [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
#this is my rule
RewriteCond %{QUERY_STRING} ^id=(\d+)$
RewriteRule /mysite.com/section/$1? [L]
RewriteRule ^section/(\d+)$ mysite.com/section/?id=$1
obviosly this doesn't work. am I missing a step?
Change
RewriteRule ^section/(\d+)$ mysite.com/section/?id=$1
to
RewriteRule ^section/(.*)$ mysite.com/section/?id=$1
so it matches anything (.*) after 'section/', not just digits (\d+)
Also, you should add an [L] flag to all RewriteRules that don't currently have it to make things a little bit more efficient.
my html with php looks like this:
in the web browser looks like:
mysite.com/section/?idN=alfanum
my rule looks like:
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule /mysite.com/section/$1? [L]
RewriteRule ^section/(.*)$ mysite.com/section/?id=$1
with this I only get URL like mysite/section/nosensealfanum
it is possible to get the title of new by the htaccess? or I its need to make changes in the way I get the new from the db?
thanks

.htaccess rewrite query string as path

I've searched for this question but I only come across really specific answers that seem difficult to tailor to my specific needs.
Let's say the URL I'm attempting to rewrite is this:
http://www.example.org/test.php?whatever=something
I want to rewrite it so that it appears as this:
http://www.example.org/test/something
How can I do this?
In order to route a request like /test/something to internally rewrite so that the content at /test.php?whatever=something gets served, you would use these rules in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?test/(.*?)/?$ /test.php?whatever=$1 [L]
And in order to redirect the query string URL to the nicer looking one:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php\?whatever=([^\&\ ]+)
RewriteRule ^/?test\.php$ /test/%1? [L,R=301]