.htaccess redirect query to url - php

so I have a php query url like so
https://domain.tld/dl.php?device=dumpling
now I would like to be able to go on a nicer url like below to also redirect at same url as above
https://domain.tld/dl/dumpling
so by accessing both urls, would get same page
I know this is possible via apache .htaccess yet can't seem to figure out the proper values there
what I tried so far is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?dl/(.*?)/?$ /dl.php?device=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /dl\.php\?device=([^\&\ ]+)
RewriteRule ^/?dl\.php$ /dl/%1? [L,R=301]
Now by accessing https://domain.tld/dl.php?device=dumpling I do get redirected to https://domain.tld/dl/dumpling, however this returns a 404 page
Think it tries to go on /dl/dumpling folder that is indeed missing
I want only https://domain.tld/dl/dumpling to work and also return my query
What the heck am I doing wrong here?

Related

Is there a way to create a friendly url like site.com/state/127 and still point to a particular file like index.php

I am creating a rest api endpoint to input data into a database. I have been trying to rewrite the url into this format http://example.com/src/public/endpoint/data. The public in the url is a folder that had has an index.php file that all the urls will be routed to but it is not working. Below is my .htaccess code.
RewriteEngine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://example.com/src/public/index.php?path=$1 [NC,L,QSA]
I am getting the link address http://example.com/src/public/?path=state/127&_=1579497796272 as the endpoint when i expect example.com/src/public/state/127. I know am not doing something correctly but i can not figure it out.
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} \?path=(.*)&(.*)\sHTTP.*$
RewriteRule ^(.*)$ /src/public/%1? [L,R]
RewriteRule ^src/public/state/127 /src/public/?path=state/127&_=1579497796272 [L]
First you should add on after RewriteEngine which means enabling mod_rewrite .
The second line will captrue the request and get a part of query string.
Third line will externally redirect the request to be friendly.
The last line will redirect a new one, internally , to the correct path.
Note: if it is ok , change R to R=301 to be permenant redirection.

htaccess - redirect GET parameters

I have a website which contains friendly URL's like localhost/article/xyz instead of localhost/article.php?name=xyz. It all works fine, but i discovered that I still can access the site also by typing "localhost/article.php?name=xyz". Is there a way to redirect it automatically to friendly URL?
RewriteEngine on
RewriteRule ^article/([a-z,-]+)$ article.php?name=$1&
Just want to redirect that: "localhost/article.php?name=xyz" to "localhost/article/xyz".
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^article/(.*)$ article.php?q=$1 [QSA]
For more information on how to use mod_rewrite check out the doc
https://httpd.apache.org/docs/current/mod/mod_rewrite.html

Redirect to a URL with .htaccess

My .htaccess file currently looks something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([^/]*)/$ ?page=$1 [L]
It makes URLs friendly from something like domain.com/?page=2 to domain.com/page/2/.
However, is there a way that I can edit this .htaccess file to make it redirect URLs containing ?page=2 (or any page number) to it's nicely formatted URL (/page/2/) automatically? My current one just allows the friendly URL version to "exist", but in no way enforces a redirect to it.
Also, is there a way I can redirect ?page=1 or /page/1/ just to the main directory/home?
EDIT:
After using what I received as an answer from Jon Lin, I was able to solve the second part of my question. Considering that ?page=1 automatically redirected to /page/1/, all I had to do was redirect /page/1/ to the homepage:
RewriteCond %{THE_REQUEST} \ /+DIRECTORY/(?:page|)/1/?
RewriteRule ^ /DIRECTORY/? [L,R]
Add this right below the RewriteEngine on line:
RewriteCond %{THE_REQUEST} \ /+(?:index\.php|)\?page=([0-9]+)
RewriteRule ^ /page/%1/? [L,R]

Change URLs with .htaccess

I have a website that I'm trying to change the URLs on. All of the URLs start with http://domain.com/?
For example, http://domain.com/?index
I just want to remove the question mark. I don't care if it appears in the address bar, I just want my users to be able to access the pages on the site without having to type the question marks.
So, if a user wants to access http://domain.com/?index, I want them to be able to access it by typing http://domain.com/index.
Is this possible using .htaccess?
I've searched around and tried a few different things for a few days now and still can't figure out a way to accomplish what I'm trying to do.
Any help is appreciated.
Thanks.
Try:
RewriteEngine On
# Match against the request instead of the URI
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?([^&\ ]+)&?([^\ ]*)
RewriteRule ^$ /%1?%2 [L,R=301]
This takes URI's like http://example.com/?path/to/file.txt and redirects the browser to http://example.com/path/to/file.txt. The browser will display that URL in the location bar instead. This is, of course, assuming that if someone actually goes to that URL, that there is something there to be served other than a 404.
EDIT
To internally map none-query string URLs to the one with a query string:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /?$1 [L]
Try using :
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ /?$1 [NC]
in your .htaccess file ,
let me know if it works.

.htaccess losing URL parameters/$_GET and $_POST after redirect

I have a php file like: http://www.domain.com/?mod=test&act=view
And I want to create a redirection from that address to something like: http://www.domain.com/view-test
so that everytime a user accesses the first uri it gets redirected to http://www.domain.com/view-test viewing the content of the first uri.
I have the following rules:
RewriteCond %{QUERY_STRING} mod=test&act=view
RewriteRule ^$ view-test? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^view-test.html amas/index.php?mod=test&act=view [L]
This works fine for pages without parameters or forms submissions but If I have any of those nothing works.
Meaning that if I have a form that is submitting to the same file it won't work. If i have something like http://domain.com/?mod=test&act=view&order_by=id i'm left with the redirected to uri and the order_by parameter is ignored!
Is it even possible to do what I'm trying? I don't really know much about this and to be honest I'm lost between all the info I find... :/
Use Query String Append:
RewriteRule ^view-test.html amas/index.php?mod=test&act=view [L,QSA]

Categories