htaccess - pretty url with multiple variables - apache

sky
something, 9, anything are variables
in address bar I want to see this:
...host/something/9/anything
so - without sky.php at all
reverse rule - from pretty to ugly - is solved - but if you have any notice - pls do:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /sky.php?c=$1&id=$2&s=$3 [L]

With your shown samples, please try following. This considers that you want to hit URL like: http://localhost:80/sky.php?c=something&id=9&s=anything in browser which should be redirected to http://localhost:80/something/9/anything here.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##This rule handles to change from non-friendly URL to friendly URL in browser.
RewriteCond %{THE_REQUEST} \s/(?:[^.]*)\.php\?c=([^&]*)&id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
##This rule internally rewrites friendly url to non-friendly url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ sky.php?c=$1&id=$2&s=$3 [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]

.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]

beautify url from art.php?id=51 to art/51

want to beautify an url from:
https://abuena.net/art.php?id=51
to
https://abuena.net/art/51
here is my try, without success, simply is ignored
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_]+)$ /$1/ [R]
RewriteRule ^([a-zA-Z0-9_]+)/$ /art.php?id=$1
My hosting provider says that my server is not Apache but LSWS - LiteSpeed WebServer - and that is probably problem in my syntax.
Any help?
Could you please try following. Considering that you need to hit URLs like https://abuena.net/art/51 in your browser. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ $1.php?id=$2
EDIT: In case you are hitting URL like https://abuena.net/art.php?id=51 in your browser then try following once.
RewriteEngine ON
RewriteCond %{REQUEST_URI} \s/([^.]*)\.php\?id=(\d+)\s [NC]
RewriteRule ^ http://%{HTTP_HOST}/%1/%2 [R=301]
RewriteRule ^([^/]*)/(.*)/?$ $1.php?id=$2 [L]

Mask URL rewrite in htaccess

I have a wordpress website in project and I want to mask the URL of all the pages so that when accessing them:
https://myweb.com/survey/page1
https://myweb.com/survey/page2
....
is displayed as:
https://myweb.com/survey/portal
I have this on .htaccess but it doesn't work:
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /survey/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /survey/index.php [L]
RewriteRule ^survey/?$ /survey/portal/
Thank you all for your time.
Very nice efforts first of all, could you please try following; written based on your shown samples. Please clear your browser cache before testing your URLs.
Also please your .htaccess file just one level above survey folder.
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /survey/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://%{HTTP_HOST}/portal [R=301]
RewriteRule ^ survey/index.php [L]
There is a common misconception that rewrite rules make ugly URLs pretty; it is more correct to say that they make externally pretty URLs internally ugly.
That's because when the browser sends a request to the web server, the web server can decide what response to serve, but it can't change what the browser sent - that's already happened.
So if you type https://myweb.com/survey/portal into a web browser, the URL sent to the server at myweb.com will be /survey/portal. Your rewrite rules decide what to do when receiving that URL, so your rule might look like this:
RewriteRule ^survey/portal$ survey/index.php
On the left is the URL the browser sent; on the right is what to serve. But notice that it wouldn't make sense to write this:
RewriteRule ^survey/portal$ survey/page1.php
RewriteRule ^survey/portal$ survey/page2.php
RewriteRule ^survey/portal$ survey/page3.php
These all match the same URL, and that URL is all we have to go on, so there is no way to map one URL to multiple pages using this mechanism. You would need something somewhere else to know which page of the survey the user is on.
You can of course do the opposite - match multiple URLs in the browser to the same resource internally:
RewriteRule ^survey/page1$ survey/index.php
RewriteRule ^survey/page2$ survey/index.php
RewriteRule ^survey/page3$ survey/index.php
Or you can map them to slightly different resources:
RewriteRule ^survey/page1$ survey/index.php?page=1
RewriteRule ^survey/page2$ survey/index.php?page=2
RewriteRule ^survey/page3$ survey/index.php?page=3
And you can use patterns and placeholders to avoid having to list out all the possibilities, so that last example can be shortened to:
RewriteRule ^survey/page([1-3])$ survey/index.php?page=$1