.htaccess rewrite query string as path - apache

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]

Related

.htaccess rewrite query string as path url

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]

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]

htaccess rewrite with 4 query string

I'm trying to rewrite my URLs but i'm so rusty with htaccess.
The url I must to rewrite is something like
http://api.example.com/endpoint.php?publicKey=VALUE1&secretKey=VALUE2&format=VALUE3&callback=VALUE4
Must to rewrite in
http://api.example.com/VALUE1/VALUE2/VALUE3/VALUE4
So here's what I've done with my htaccessfile:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /endpoint.php?publicKey=$1&secretKey=$2&format=$3&callback=$4 [L]
This isn't working as expected cause I can't read one of these variables in PHP. Even that, I can't reach the page if I don't put endpoint.php before the parameters eg: http://api.example.com/endpoint.php/... that's not the
needed behavior. How I can rewrite the URLs as expected?

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 url rewrite help

I have the following rewrite URL:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs
RewriteRule ^(.*)$ index.php?page=$1 [PT,L]
Now I want to add an exception, that if the URL is something like mysite.com/abc it should ignore it and all things inside it also. mysite.com/abc/dfgs also should be excluded from this rewriting.
How can I accomplish this?
If /abc is an existing directory, you can put another .htaccess file in there with
RewriteEngine Off
If it's really just one string you want to not rewrite (whether it exists or not)
RewriteCond %{REQUEST_URI} !^/abc
will not rewrite if the requested path starts with "/abc"
To test rewrite rules without messing up the site for regular browsers (not that you should be editing in a live environment of course, this is purely hypothetical :-) ), I've found the following very helpful:
RewriteCond %{REMOTE_ADDR} 12.34.56.78 # <-- where that's your IP address
This should avoid rewriting if the URI contains abc. This may or may not be exactly what you want. If it isn't edit your question.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#Rewrite ONLY if the REQUEST does NOT contain abc
RewriteCond %{REQUEST_URI} !abc
# Rewrite all other URLs
RewriteRule ^(.*)$ index.php?page=$1 [PT,L]