Redirect not working in htaccess - .htaccess

I have a link like this:
/add.php?id=50&link=page2
I want it to redirect to this:
/add/50/page2
How can I do this? I tried the following, but it did not work:
Redirect /add.php?id=50&link=page2 /add/50/page2

You can use this code in your DOCUMENT_ROOT/.htaccess file:
Options -MultiViews
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /add\.php\?id=([^\s&]+)&link=([^\s&]+) [NC]
RewriteRule ^ add/%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^add/([^/.]+)/([^/.]+)/?$ add.php?id=$1&link=$2 [L,QSA,NC]

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]

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]

mod_rewrite: url rewrite and redirect

I have the following URL:
http://example.com/pages/cms/impressum.php
and want to get an URL like this:
http://example.com/impressum
My rewrite Rule is:
RewriteEngine on
RewriteBase /
RewriteRule ^impressum$ /pages/cms/impressum.php [R,L]
The Problem is, I can open the url in both ways. I would like a forwarding from /pages/cms/impressum.php to /impressum. If I use [R=301], the second URL which I do not want works. I want to reverse this rule.
Use:
RewriteEngine On
RewriteBase /
# To externally redirect pages/cms/impressum.php to impressum
RewriteCond %{THE_REQUEST} \s/+pages/cms/impressum\.php[\s?] [NC]
RewriteRule ^ /impressum [R=301,L]
# To internally forward impressum to pages/cms/impressum.php
RewriteRule ^impressum/?$ pages/cms/impressum.php [L,NC]

How to create clean url using .htaccess for multiple parameters

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]

mod_rewrite url using .htaccess

What I am trying to do:
Change
www.mysitename.com/pages/about
to
www.mysitename.com/about
What I have tried so far:
RewriteEngine on
RewriteRule ^(.*)$ pages/$1
but when I go onto my website and click the about section, the url is still www.**.com/pages/about. htaccess is enabled on my server, so that's not the problem.
What am I doing wrong?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# change URL in browser from /pages/about to /about
RewriteCond %{THE_REQUEST} \s/+pages/(\S*) [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# internally forward /about to /pages/about
RewriteRule ^((?!pages/).+)$ pages/$1 [L,NC]

Resources