URL rewriting by htaccess - apache

I would like to rewrite URLs using htaccess like, in the example below.
My HTTP request is:
http://example.com/store1/index.php?page=user/test
I would like to get the URL in browser as:
http://example.com/store1/user/test.**
Also, please note, that the 1st parameter followed by the slashes after example.com is not a real folder; so I am already rewriting it, using this htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]+[^/](.*)$ /$1 [QSA]
and it works.
But I need to achive the˛htaccess rewriting, without affecting the above settings.

Related

How to make .htaccess RewriteRule that support path and query_string at the same time

I need help with .htaccess on RewriteRule. I already have some URL that are using the RewriteRule like:
https://example.com/content/2/about_us
This URL above, I can successfully redirect to page.php and capture variable like id=2 and pagename=about_us
But there are URL that I would like to have the format about and normal query_string at the same time like the following:
https://example.com/page?keyword=battery&sort=asc&pagenum=1
This URL above would redirect to page.php and I want to capture the query_string. Those query_string would not limit to keyword, sort, pagenum. It can be any other name. At the moment I cannot capture the query_string at all.
At the moment I can only redirect the URL https://example.com/page to page.php with the following:
RewriteRule ^page/?$ page.php [L]
Anyone can help?
With your shown samples and attempts please try following .htaccess rules file.
Please make sure:
Place your .htaccess rules file along with content folder.
Clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /content/2/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(content/2)/([^/]*)/?$ $1/$2.php?tag=battery&sort=asc [NC,QSA,L]

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]

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?

URL rewrite rule that allows long URL to appear shorter

I would like to users to visit:
domain.com/example
and have the content from the following page displayed:
domain.com/directory1/directory2/directory3/example.html
In the browser, I would like it to say domain.com/example without redirecting. We have a series of landing pages that need to have short URLs within the domain.
I considered doing something programmatically such as a PHP include however I felt an .htaccess rewrite rule would be best practice.
So you want:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^example$ /directory1/directory2/directory3/example.html [L]
If you want it to work for every file (replacing "example" with any file) then replace the RewriteRule line with:
RewriteRule ^([^/]+)$ /directory1/directory2/directory3/$1.html [L]