How to create clean url using .htaccess for multiple parameters - .htaccess

This is my .htaccess code to rewrite clean url.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+download\.php\?id=([^\s&]+) [NC]
RewriteRule ^ download/%1? [R=301,L]
RewriteRule ^download/([^/]+)/?$ download.php?id=$1 [L,QSA]
This code works fine for single parameter in url. For example it rewrites www.mysitename.com/download.php?id=123 to www.mysitename.com/download/123
But when I tried to pass multiple parameters in url, all I got are errors. Searched various resources and related questions but didn't got proper solution.
I need a url like www.mysitename.com/download/123/file-name instead of www.mysitename.com/download.php?id=123&name=file-name
I guess I've to use something thing like this RewriteRule ^(.*)/(.*)$ download.php?id=$1&name=$2. But while implementing I'm getting 404 error. How can I alter My code to pass multiple urls. Thanks in advance.

You just need to add a parameter in your rule
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+download\.php\?id=([0-9]+)&name=([^\s&]+)\s [NC]
RewriteRule ^ download/%1/%2? [R=301,L]
RewriteRule ^download/([0-9]+)/([^/]+)/?$ download.php?id=$1&name=$2 [L]

Related

How to write htaccess rewrite rule for change link

i'm searching for rule for change link in htaccess.
Currently the link looks like this:
localhost.com/forum/profile.php?id=1
And this is the effect I would like to achieve:
localhost.com/forum/profile/1
I added the following rules:
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/forum/profile\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /forum/profile/%1? [R=301,L]
The problem is that the page automatically redirects to the expected link, but a 404 error is returned
You need one more rule to map forum/profile/123 to forum/profile\.php?id=123. Please clear your browser cache before testing your URLs.
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/forum/profile\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /forum/profile/%1? [R=301,L]
RewriteRule ^forum/profile/([0-9]+)/?$ /forum/profile.php?id=$1 [NC,QSA,L]

How could I rewrite urls properly?

I have URLs like http://url.com/top_admin/order.php?do=view&oid=124 and
I need to rewrite it to http://url.com/top_admin/order/view/124 , where
top_admin is a folder that contains my script.
this is my code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^order/(.*)/([0-9]+) order.php?do=$1&oid=$2 [L]
but it does not work.
With your shown samples/attempts, please try following. Please make sure to clear your browser cache before testing your URLs.
Options +FollowSymLinks -MultiViews
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(top_admin/order)\.php\?do=([^&]*)&oid=(\d+)\s [NC]
##Rule for external rewrite with 301.
RewriteRule ^ /%1/%2/%3? [R=301,L]
##Rule for internal rewrite to .php file.
RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ $1.php?do=$2&oid=$3 [L]

RewriteRule for 'pretty links' not working

I am trying to make a link that looks like https://www.exapmle.com/profile.php?u=8 to look like https://www.exapmle.com/profile/8
I have a tried variations of this in htaccess:
RewriteRule ^/profile/([0-9]+)/?$ profile.php?u=$1
RewriteRule ^/profile/([0-9]+)\.html /profile.php?u=$1
I don't know what i am doing wrong, the links don't change and I'm not getting any errors either
You may use this code in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /profile\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /profile/%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^/?profile/(\d+)(?:\.html)?/?$ profile.php?u=$1 [L,QSA,NC]

Not redirecting for clean URL

So I'm running into an issue here that I believe I have correct yet it's just not working for me here.
So simple enough I want me current URL which is:
tremorelights.com/chandelier-c-18.html?page=2&osCsid=vtemi4nqlioftbteam6nap0s77
To look like this URL:
tremorelights.com/chandelier/2/
So I'm trying to redirect it to the clean URL and this is what I have now.
RewriteEngine On
RewriteBase /
RewriteRule ^chandelier-c-18.html?page=$1 chandeliers/([0-9]+) [QSA,L,R=301]
Yet this is not working at all. When I manually input the clean URL it does not give me a 404 error but it does not take me to any other page like it should.
You can match against the query string in a rewrite rule, what you want to match against is the %{THE_REQUEST} variable here.
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \ /+chandelier-c-18.html\?page=([0-9]+)
RewriteRule ^ chandeliers/%1? [QSA,L,R=301]
then maybe:
RewriteRule ^chandeliers/([0-9]+) chandelier-c-18.html?page=$1 [L]
Change your rule to this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^chandeliers/([0-9]+)/?$ chandelier-c-18.html?page=$1&osCsid=vtemi4nqlioftbteam6nap0s77 [QSA,L,NC]

mod_rewrite rule for exact expression not working

I don't work to much with mod_rewrite so I could easily be just missing something simple. I want to match and replace a specific URL. The URL I want to rewrite would look like this:
http://www.sample.com/hello to http://www.sample.com/index.php?page=hello
My htaccess currently has the following rule in place:
RewriteRule ^(hello)/?$ index.php?page=$1 [L]
However, when I implement the rule and I test it by trying to go to http://www.sample.com/hello or http://www.sample.com/hello/ I always get my 404 error document. ugh I feel like it should be simple and I must be missing something right there. Any help would be fantastic and much appreciated!
Here is my whole file for reference:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?sample\.com/ [NC]
RewriteCond %{REQUEST_URI} !hotlink\.(gif|png) [NC]
RewriteRule ^(.*)\.(gif|jpe?g|png)$ sample.com/hotlink.png[R,NC,L]
RewriteCond %{HTTP_HOST} ^www.sample\.com$ [NC]
RewriteRule ^(.*)$ http://sample.com/$1 [R=301,L]
RewriteRule ^(hello)/?$ index.php?page=$1 [L]
Thanks
Your code works perfeclty fine for me. Maybe you're missing
RewriteEngine On
RewriteRule ^(hello)/?$ page.php?page=$1 [L]
or maybe mod_rewrite is not installed or loaded at all.
Look for
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
in your main apache configuration file httpd.conf
This is one of those times...
I cleared the htaccess file and only used the following couple lines and tested it out:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
#
RewriteRule ^(hello)/?$ page.php?page=$1 [L]
I realized looking at the non custom 404 page that it referenced page.php and not index.php!
Not Found
The requested URL /page.php was not found on this server.
So turns out with all my edits I mistakenly typed page.php in the rule instead of index.php. I corrected it and the rule works. One of those times :)
Thanks all!

Resources