mod_rewrite url using .htaccess - .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]

Related

how to hide id = from url using htaccess file

I need the url
from
localhost/project/category?c=electronics
to
localhost/project/category/electronics
I have tried
RewriteRule ^category/([^/\.]+)?$ /category.php?c=$1 [L]
RewriteRule ^category/+?$ /category.php?c=$1 [NC,L]
With your shown samples and attempts please try following htaccess rules. Please do clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
##External redirect to url change in browser.
RewriteCond %{THE_REQUEST} \s/(project/category)\.php\?c=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Internal rewrite to category.php in backend.
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ %{DOCUMENT_ROOT}/$1/$2.php?c=$3 [QSA,L]
RewriteEngine on
RewriteBase /
RewriteRule ^project/category/([0-9a-z]+)$ /project/category?c=$1 [L]
Why is "project/" missing in your original try ?
You have to specify the full path.
You can try this simple rewriteRule wich should works.

How to Rewrite URL in htaccess

I have URLs with two query parameters e.g. /skills/keywords/list.php?industry=retail&q=analyst and I'd like to redirect those URLs to /list-of-%2-skills-in-%1 .
I have tried the code below which looks at the query "industry" and "q" to build the destination URL: /list-of-%2-skills-in-%1
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^industry=(.*)&q=(.*)$ [NC]
RewriteRule ^skills/keywords/list\.php$ /list-of-%2-skills-in-%1? [L,R=301]
I'm expecting: /skills/keywords/list.php?industry=retail&q=analyst to redirect to /list-of-analyst-skills-in-retail which it does but I have a 404 content not found.
Can someone point me to the right direction please?
This is because the destination path /list-of-%2-skills-in-%1 doesn't exist on your server. You need to rewrite this path to the existent file /skills/keywords/list.php?industry=retail&q=analyst .
RewriteEngine On
RewriteBase /
#redirect /skills/keywords/list.php?industry=foo&q=bar
#to /list-of-bar-skills-in-foo
RewriteCond %{THE_REQUEST} /skills/keywords/list\.php\?industry=([^&]*)&q=([^\s]+) [NC]
RewriteRule ^skills/keywords/list\.php$ /list-of-%2-skills-in-%1? [L,R=301]
#rewrite new URL to the old one
RewriteRule ^list-of-([^-]+)-skills-in-([^-]+)/?$ /skills/keywords/list.php?industry=$1&q=$2 [NC,L]

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]

php - Htaccess as fake directories

I am making a mini blog that could make it's url looks like this:
From: http://127.0.0.1/index.php?post=the-story-of-us
To: http://127.0.0.1/view/the-story-of-us
I have tried this but i'm getting 404 not found.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?post=([^&]+)
RewriteRule ^ /view/%2/? [L,R=301]
Your current rule only handles the case: Redirect old url to new url.
(By the way, +1 for using THE_REQUEST to avoid a redirect loop)
You also need to handle the case: Rewrite (internally) new url to old url.
Here is how your htaccess should look like
RewriteEngine On
# Redirect /index.php?post=XXX to /view/XXX
RewriteCond %{THE_REQUEST} \s/index\.php\?post=([^&\s]+)\s [NC]
RewriteRule ^ /view/%1? [L,R=301]
# Internally rewrite back /view/XXX to /index.php?post=XXX
RewriteRule ^view/([^/]+)$ /index.php?post=$1 [L]
I do not udnerstand your RewriteCondition, but the RewriteRule should look like this:
RewriteEngine on
RewriteBase /
RewriteRule ^view/(.*)/? ./index.php?post=$1 [L,R=301]

Redirect not working in 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]

Resources