I have 2 type of url in my page.
1- http://www.example.com/index.php?id=12
I want always this:
http://www.example.com/12
But for anything else like:
2- http://www.example.com/index.php?id=12&id2=123&asd=qwe&svd=sdf...
I want it with no change:
http://www.example.com/?id=12&id2=123&asd=qwe&svd=sdf...
SO I want only redirect for http://www.example.com/index.php?id=12
I try this
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
it works only for first (http://www.example.com/index.php?id=12)
but for second,it redirect to (http://www.example.com/***)
I read in stackoveflow I must use [QSA,L]
So I try this:
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [QSA,L]
But I got 500 error (Internal Server Error)
this is my complete code:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
---EDIT ---
I could do it with this
RewriteRule ^index.php/([^/]+)/?([^/]*) /index.php?id=$1 [NC]
for example www.example.com/123 works!
but www.example.com/index.php?id=123 not redirect to www.example.com/12
To convert http://example.com/index.php?id=foo to http://example.com/foo you can use the following rules in root/.htaccesso :
RewriteEngine on
#1)Redirect "/index.php?id=foo" to "/foo"#
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+)\sHTTP [NC]
RewriteRule ^ /%1? [L,R]
#2)The rule bellow will internally map "/foo" to "/index.php?id=foo"#
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /index.php?id=$1 [L]
[Tested] This works on my apache server.
Related
with this code and clean URL like (example.com) I could add www and remove id in url.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1 [L]
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But now my root of website is in a folder.
so my new url is www.examole.com/newfolder
how can add newfolder to top code?
You can use this .htaccess in /newfolder:
RewriteEngine On
RewriteBase /newfolder/
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ %1%2? [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]
hello all i have a url like
http://www.domain.com/dash?do=about&w=me
but i would like to make it like
http://www.domain.com/dash/about/me
i have these things in my htaccess file.
RewriteEngine On
RewriteBase /
Options All -Indexes
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]
ErrorDocument 403 /www.domain.com/error404.php
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# redirect /profile?eid=1 to /eid/1
RewriteCond %{THE_REQUEST} \s/+profile(?:\.php)?\?(eid)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internally rewrite /eid/1 to /profile.php?eid=1
RewriteRule ^(eid)/(\d+)$ profile.php?$1=$2 [L,QSA,NC]
# redirect /dept?did=1 to /did/1
RewriteCond %{THE_REQUEST} \s/+dept(?:\.php)?\?(did)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internally rewrite /did/1 to /dept.php?did=1
RewriteRule ^(did)/(\d+)$ dept.php?$1=$2 [L,QSA,NC]
# redirect /job-details?job=1 to /job/1
RewriteCond %{THE_REQUEST} \s/+job-details(?:\.php)?\?(job)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internally rewrite /job/1 to /job-details.php?job=1
RewriteRule ^(job)/(\d+)$ job-details.php?$1=$2 [L,QSA,NC]
RewriteCond %{THE_REQUEST} \s/+enterprise\.php\?url=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ enterprise.php?url=$1 [L,QSA]
can any one suggest something to achieve this i have ?do=about&w=me comon everywhere and just need to do it like /about/me after the page.
It is slowly becoming difficult to maintain due to so many rules but here it is:
ErrorDocument 403 /www.domain.com/error404.php
Options All -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# redirect /profile?eid=1 to /eid/1
RewriteCond %{THE_REQUEST} \s/+profile(?:\.php)?\?(eid)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internally rewrite /eid/1 to /profile.php?eid=1
RewriteRule ^(eid)/(\d+)$ profile.php?$1=$2 [L,QSA,NC]
# redirect /dept?did=1 to /did/1
RewriteCond %{THE_REQUEST} \s/+dept(?:\.php)?\?(did)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internally rewrite /did/1 to /dept.php?did=1
RewriteRule ^(did)/(\d+)$ dept.php?$1=$2 [L,QSA,NC]
# redirect /job-details?job=1 to /job/1
RewriteCond %{THE_REQUEST} \s/+job-details(?:\.php)?\?(job)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internally rewrite /job/1 to /job-details.php?job=1
RewriteRule ^(job)/(\d+)$ job-details.php?$1=$2 [L,QSA,NC]
RewriteCond %{THE_REQUEST} \s/+enterprise\.php\?url=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# /dash?do=about&w=me => /dash/about/me
RewriteCond %{THE_REQUEST} \s/+([^?]+)(?:.php)?\?do=([^&\s]+)&w=([^&\s]+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ $1.php?do=$2&w=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ enterprise.php?url=$1 [L,QSA]
current URL:
domain/index.php?id=p123
I want:
add www
remove:index.php
remove 'id' form URL
I do it this way:
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1 [L]
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
it works if domian is without sub directory.
how can I change this code to support sub directory too like this:
domian/subdirectory/index.php?id=p123
I'll go about it this way:
First, redirect non-www URLs to www ones:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Rewrite the friendly URLs to respective index.php files:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*/)?([^/]+)$ /$1index.php?id=$2 [L]
Next, handle the raw URIs with index.php in them:
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
The last rule will also take care of URLs like domain.com/dir/?id=sth and domain.com/dir/index.php?id=sth
You can accomplish this with the following changes:
RewriteCond %{THE_REQUEST} \ /(.*/|)(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^(.*/|)([^/]*)$ $1index.php?id=$2 [L]
We know that %{THE_REQUEST} has the format GET /path/to/index.php?id=12312412 HTTP/1.1 or something along those lines. In the first rule we match \, which is a literal space, then the beginning slash that is always there. (.*/|) will match everything until index.php, or exactly nothing. The same trick we do in the second rule. I have added RewriteCond %{REQUEST_URI} !index\.php to prevent an infinite loop when index.php does not exist for a particular directory.
I use this htaccess for multiple parameters like bank, state,district & branch.
But it only format bank not others...???
I need it for:
www.domain.com/bank/ .....(It works)
www.domain.com/bank/state/
www.domain.com/bank/state/district/
www.domain.com/bank/state/district/branch/
My htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?bank=([^&\s]+)
RewriteRule ^ /%2/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?bank= ([^&]+)&state=([^&]+)
RewriteRule ^ /%2/%3/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?bank=([^&]+)&state=([^&]+)&district=([^&]+)
RewriteRule ^ /%2/%3/%4/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?bank=([^&]+)&state=([^&]+)&district=([^&]+)&branch=([^&]+)
RewriteRule ^ /%2/%3/%4/%5/? [L,R=301]
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
Kindly provide solution for this...Thanks & Regards.
You need to tweak your regex and reverse order of redirect rules:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index\.php\?bank=([^&\s]+)&state=([^&\s]+)&district=([^&\s]+)&branch=([^&\s]+)\s
RewriteRule ^ /%2/%3/%4/%5/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index\.php\?bank=([^&\s]+)&state=([^&\s]+)&district=([^&\s]+)\s
RewriteRule ^ /%2/%3/%4/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index\.php\?bank= ([^&\s]+)&state=([^&\s]+)\s
RewriteRule ^ /%2/%3/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index\.php\?bank=([^&\s]+)\s
RewriteRule ^ /%2/? [L,R=301]
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.+)$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
Make sure to clear your browser cache before testing this change.
I would like to convert URLs like subdomain.domain.fr to domain.fr/index.php/subdomain
I tried with that htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain.fr$ [NC]
RewriteRule ^(.*)$ /index.php/%1 [L,QSA]
</IfModule>
With this, my site responds with domain.fr but I get a Internal Server Error with subdomain.domain.fr
Any idea on what I'm doing wrong ?
You're probably getting looping error. You can use this code to avoid it:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^.]+).domain\.fr$ [NC]
RewriteRule ^ /index.php/%1 [L]