301 Redirect Dynamic URL - .htaccess

OK so I have the following as part of the URL: /user/26872-essi/
and I'm going to final URL of /essi/
I currently have:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^user\/([0-9]+)\-.+[/]?$ /user.php?user_id=$1
So I tried:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^user\/([0-9]+)\-.+[/]?$ /$2/ [R=301,L]
RewriteRule ^(((?!\.|\/).){1,})\/?$ /user.php?username=$1
It doesn't seem to be passing the variable in. Any ideas what I could be doing wrong?

You may use these rules in your site root .htaccess:
RewriteEngine On
RewriteRule ^user/\d+-([\w-]+)/?$ /$1/ [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ user.php?username=$1 [L,QSA]

Related

Trying to use .htaccess for www and https redirects

I'm trying to get my site to redirect to HTTPS, and I can only seem to get it partially working.
Here's what my .htaccess currently looks like:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]
The problem is, is that it's not retaining the URL (virtual path that gets parsed by index.php), so for example, http://blah.com/mypage redirects to https://blah.com
What am I doing wrong?
I also need to ensure www is in place as well, but one step at a time :)
You can use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blah.com$
RewriteRule ^$ https://blah.com/mypage [L,R=301]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^https://blah.com$
RewriteRule ^$ https://blah.com/mypage [L,R=301]
So my file works - I just didn't clear my cache. Major oops on my end.
Here's the .htaccess file for those curious:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]

url rewriting with QUERY_STRING

I would like to rewrite http://example.com/test/index.php?id=123
to http://example.com/test/item/123/ and http://example.com/test/index.php?id=123 redirect to the human friendly url.
I don't know what is wrong in my htaccess :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /test/
RewriteCond %{QUERY_STRING} ^id=([a-z0-9]+)$ [NC]
RewriteRule ^/item/%1/? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^item/([a-z0-9]+)/$ index.php?id=$1 [L]
You can use the following RewriteRule in your /test/.htaccess file
Options +FollowSymLinks -MultiViews
RewriteEngine on
#redirect old url to the new one
RewriteCond %{THE_REQUEST} /test/index\.php\?id=([^\s]+) [NC]
RewriteRule ^ /test/item/%1? [L,R=301]
#internally map new url to the old one
RewriteRule ^item/(.+)/?$ /test/index.php?id=$1 [L,NC]

.htaccess url: rewrite is not working at local host Xampp

My current url is
http://localhost/www.zeeshan.com/user.php?name=zeeshan06
I want to display my url as
http://localhost/www.zeeshan.com/user/zeeshan06
I use the following code but it not working
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Options +FollowSymlinks
RewriteBase /
RewriteRule ^user/(.*)user.php?name=$1 [R]
Please help me to solve my problem. Thanks.
If you place this .htaccess inside the folder www.zeeshan.com:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /www.zeeshan.com/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([^/]+)$ user.php?name=$1 [NC,L]
And access the URL:
http://localhost/www.zeeshan.com/user/zeeshan06
It should lead you to:
http://localhost/www.zeeshan.com/user.php?name=zeeshan06
// http://localhost/Milan_Practice/Milan_Mvc/routing.php?name=about
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /Milan_Practice/Milan_Mvc/routing\.php\?name=([^&]+)
RewriteRule ^ /Milan_Practice/Milan_Mvc/routing/%2/%3?%4 [L,R=301]
RewriteRule ^/?Milan_Practice/Milan_Mvc/routing/([0-9]+)/(.*)$ /Milan_Practice/Milan_Mvc/routing.php?name=$1 [L,QSA]

Rewrite with htaccess not working

i have a site wich have a dynamic url for 3 pages only. and for that i dont go for php function. So I decided to go for .htaccess rewrite rules but I am not having any luck yet.
This is my actual url: /index.php?mode=service&inid=1
I want to rewrite it to this: /home-theater
I try it my self and with this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mysite\.com.au$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com.au/$1 [L,R=301]
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.mysite.com.au/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^home-theater/?$ index.php?mode=service&inid=1 [L,NC,QSA]
I still can't get it to work.
Also I write index.php to www redirect before this code, so I guess if it cause any issue or not. index.php to www site redirects work perfectly but this is not working.
Use L flag like this:
RewriteRule ^home-theater/?$ index.php?mode=service&inid=1 [L,NC,QSA]
Remember this will not change the URL in the browser since this will internally forward request to your index.php.
Also if this doesn't work then provide your full .htaccess in your question.
Update: I have modified your .htaccess here, please try it out now after clearing your browser cache:
DirectoryIndex index.php
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.mysite\.com.au$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com.au/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^ http://www.mysite.com.au/ [R=301,L]
RewriteRule ^home-theater/?$ index.php?mode=service&inid=1 [L,NC,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]

How to forward from example.com/en to example.com/en/ with htaccess?

I am stuck trying to write a rewrite rule for my htaccess file which should forward from for example www.example.com/en to www.example.com/en/
I tried: RewriteRule ^en/?$ en [L]
but nothing happens. Does someone have an idea how to accomplish this?
You can use following rules in your .htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?![^/]*/).*$ %{REQUEST_URI}/ [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/$ index.php?lang=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?action=$2&lang=$1 [QSA,L]
This will:
externally redirect: /en to /en/ and internally redirect this to /index.php?lang=en
internall redirect: /fr/login to /index.php?action=login&lang=fr
Additionally: As per your comment you will need this rule to add www in domain:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Use RedirectMatch:
RedirectMatch Permanent ^/en$ /en/

Resources