I want to redirect my site url's with 301 code
http://www.domain.com/index.php?ms=user&ms_1=name --> http://www.domain.com/user/name
http://www.domain.com/index.php?ms=2652&ms_1=title --> http://www.domain.com/2652/title
http://www.domain.com/index.php?ms=questions --> http://www.domain.com/questions
http://www.domain.com/index.php?ms=aaa&ms_1=bbb&ms_2=ccc --> http://www.domain.com/aaa/bbb/ccc
the stuffs after ms can have space and utf8 characters too
How can I do it ?
This should work
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?ms=(.*)&ms_1=(.*)&ms_2=(.*)\ HTTP
RewriteRule ^ /%2/%3/%4\? [R=301,L]
RewriteRule ^(.*)/(.*)/(.*)$ /index.php?ms=$1&ms_1=$2&ms_2=$3 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?ms=(.*)&ms_1=(.*)\ HTTP
RewriteRule ^ /%2/%3\? [R=301,L]
RewriteRule ^(.*)/(.*)$ /index.php?ms=$1&ms_1=$2 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?ms=(.*)\ HTTP
RewriteRule ^ /%2\? [R=301,L]
RewriteRule ^(.*)/$ /index.php?ms=$1 [L]
It will change http://www.domain.com/index.php?ms=aaa&ms_1=bbb&ms_2=ccc into http://www.domain.com/aaa/bbb/ccc, but all you to see the content of http://www.domain.com/index.php?ms=aaa&ms_1=bbb&ms_2=ccc etc for the other two links you have.
I believe you can enable Apache rewrite module and give rewrite rules to do it. It has regex with back-reference ability.
A possible (cause I'm not that familiar with regex in rewrite module) ruleset could be :
ReWriteRule ^/index.php?ms=(.+) /$1
ReWriteRule ^/index.php?ms=(.+)&ms_1=(.+) /$1/$2
ReWriteRule ^/index.php?ms=(.+)&ms_1=(.+)&ms_2=(.+) /$1/$2/$3
please follow the code i hope it will be help to you.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?ms=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?ms=$1&ms_1=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?ms=$1&ms_1=$2&ms_2=$3 [L]
befor this code apache rewrite module is enable
Related
i am using apache 2.4.9
i want to rewrite a url in htaccess
https://localhost/balance.php/editbalance
when i enter above url it should be displayed like below
https://localhost/editerapi/editbalance
i have tried this but its not working
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^editerapi/?$ balance.php [NC,L]
RewriteCond %{REQUEST_URI} ^/editerapi [NC]
RewriteRule ^.*/([^/]+)/? balance.php/$1 [L] #POST
MY REWRITE ENGINE IS ON
the below code of hiding php extension is working
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [QSA,L]
In order to show the new URL in the browser's bar, you must redirect instead of rewrite, e.g. use the R|redirect flag
RewriteRule ^balance\.php/(.*)$ /editerapi/$1 [R,L]
Now the client requests the new URL /editerapi/..., and you must rewrite to
RewriteRule ^editerapi/(.*)$ /balance.php/$1 [L]
To prevent a rewrite loop, a RewriteCond is added. Now the redirect only happens, when balance.php is requested
RewriteCond %{THE_REQUEST} " /balance\.php/"
Everything put together gives
RewriteRule ^editerapi/(.*)$ /balance.php/$1 [L]
RewriteCond %{THE_REQUEST} " /balance\.php/"
RewriteRule ^balance\.php/(.*)$ /editerapi/$1 [R,L]
I would like redirect http://example.com/index.php?a=music to http://example.com/music (where the number is a variable/dynamic). My site is http : / / example . com
I added the following line to my mod-rewrite rules in my .htaccess file:
RewriteRule ^index\.php$/?/a/=(.*)$ http://example.com/index.php?a=$1 [L,R=301]
However, it doesn't seem to work. I know I'm doing something wrong, I'm just not sure how to fix it. Any help is appreciated.
Best,
Jeff
Edited:
Thanks Tom,
This is what I have:
Rewritecond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
This should work:
RewriteRule ^(.*)$ /index.php?a=$1 [L]
RewriteEngine On
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
This is how my current .htaccess looks like at the moment. Line 3 and 4 I took from another question at stackoverflow, and the rest simply removes the .php file extension. But the redirect to https part isn't working. Any ideas?
EDIT:
I just figured out that the problem is in my site's .conf file in the sites-enabled folder of apache. I need a separate block for the :80 and :443 port, which I can't figure out how to do, but that's a topic for another question.
The following is what I use to enforce https, hope it will be of use.
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R,L]
You can try this for redirect on https:
RewriteRule ^(.*)$ https://www.%1.%2/$1 [L,R=301]
The Following code may Works.
Url:- home
RewriteRule ^home$ index.php?page_slug=$0 [QSA]
RewriteRule ^home/$ index.php?page_slug=$0 [QSA]
This is URL- page/new
RewriteRule ^page/([a-zA-Z0-9-/]+)$ page.php?page_slug=$1 [QSA]
RewriteRule ^page/([a-zA-Z0-9-/]+)/$ page.php?page_slug=$1 [QSA]
You can use the following code to redirect from http to https :
RewriteCond %{HTTPS} ^off$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NC,L,R]
I need to redirect the below url
http://localhost/CodeIgniter/index.php?/Welcome/
to
http://localhost/CodeIgniter/Welcome/
and I have tried the below in .htaccess
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
The above code taken me to below
http://localhost/CodeIgniter/?/Welcome/
I need to remove that ? mark for avoiding duplicate urls in seo. Can anybody help me
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
And you have to enable mod_rewrite
If you are using PHP version lower than 5.2.6 -> Remove the ? from index.php
You can try your code this way.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?/(.*)
RewriteRule ^ /%1? [R=302,L]
I am trying the following rules to remove file extension from file name but the problem is it is showing that it is removed but when I login again it is not redirecting to my home page.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase \
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
Thank You
In your redirection just type in page name with no extension, eg. header('Location: index'); or redirect_user('index'); and use the code below in your .htaccess file. now the link on the browser will be say "localhost/index"
RewriteEngine on
RewriteRule ^([^./]{3}[^.]*)$ /index.php?page=$1 [QSA,L]
I think your rules are just a little bit off. You are using THE_REQUEST to redirect php extension to non php extension but you are not using a / after %1 in the rewriterule so when the next rule is read it is not matching because it will only internally redirect if there is a / in that rule. So either add a forward slash after %1 in the second rule or make the backslash optional in the last rule. I would just make it optional in last rule so it will match either way.
Try this update and see how it works.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /([^&\ ]+).php [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ $1.php [NC,L]