#anubhava and #RavinderSingh13 have continued to help me learn and progress to where I am today with this script. This is what I have so far:
##1st rule with one parameter: $id--
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
##Parameters are $id and $class
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\w+-class)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(\w+-class)/?$ /directory/file.php?id=$1&class=$2 [NC,L,QSA]
##Rule with two parameters: $id and $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]```
Now, I would like to add one more rule with only one parameter instead of two. However, this parameter uses PHP $_GET['name'] in the url. I am trying to add one more rule that rewrites http://IPADDRESS.com/file.php?name=JohnDoe to http://IPADDRESS.com/newdirectory/JOHNDOE.
Thank you!
With your shown samples please try following. Please make sure to clear browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
##Parameters are $id and $class
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\w+-class)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(\w+-class)/?$ /directory/file.php?id=$1&class=$2 [NC,L,QSA]
##Rule with two parameters: $id and $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]
##Rule with two parameters: $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?name=(\S+)\s [NC]
RewriteRule ^ /directory/%1? [R=301,L]
RewriteRule ^directory/([^/]*)/?$ /directory/file.php?name=$1 [NC,L,QSA]
Related
#anubhava and #RavinderSingh13 have helped me tremendously so far in understanding more about .htaccess and rewrite rules. However, even though both the first rule (with one parameter: $id) and the second rule (with two parameters: $id and $name) works for rewriting the url, my third rule I attempted with also two parameters ($id and $class) fails, and does not rewrite the final url. Below is my entire file so far. I would like to add that the $class parameter is in the format of a-class, b-class, etc., so maybe that could be a contributor to the the rewrite not working? Thank you for helping me continue to learn thus far!
RewriteEngine on
--1st rule with one parameter: $id--
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
--2nd rule with two parameters: $id and $name--
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]
--My attempt at the third rule following the example from the second, but this rule fails. Parameters are $id and $class--
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&class=$2 [NC,L,QSA]
With your shown samples and considering class word will be there always in your 3rd url try following rules. Also you need not to write RewriteEngine ON many times, only 1 time starting of file is enough.
RewriteEngine on
##1st rule with one parameter: $id--
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
##Parameters are $id and $class
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\w+-class)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(\w+-class)/?$ /directory/file.php?id=$1&class=$2 [NC,L,QSA]
##Rule with two parameters: $id and $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]
Bonjour,
I try to redirect urls of a website
Example :
www.exemple.net/?p=2
to
www.exemple.net/index-2.html
www.exemple.net/?p=35
to
www.exemple.net/index-35.html
etc...
So i add this lines to my .htacess :
RewriteCond %{QUERY_STRING} ^(?)p=(.*)$ [NC]
RewriteRule .* /index-%1.html [L,R=301]
But i'm redirected to http://www.exemple.net/index-2.html?p=2
My .htaccess :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^exemple.net$
RewriteRule ^(.*) http://www.exemple.net/$1 [QSA,L,R=301]
RewriteCond %{QUERY_STRING} ^(?)p=(.*)$ [NC]
RewriteRule .* /index-%1.html [L,R=301]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Can you help me please ?
You need to add a question mark at the end of the destination to discard the query string:
RewriteRule .* /index-%1.html? [L,R=301]
Alternatively, use the QSD flag:
RewriteRule .* /index-%1.html [QSD,L,R=301]
Try :
#Redirecting /?p=2
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/\?p=([^&\s]+) [NC]
RewriteRule ^ /index-%1.html? [NE,NC,R,L]
#Redirecting /transfer?p=2
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/transfer\?p=([^&\s]+) [NC]
RewriteRule ^ /transfer-%1.html? [NE,NC,R,L]
#Redirecting /blog?p=2
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/blog\?p=([^&\s]+) [NC]
RewriteRule ^ /blog-%1.html? [NE,NC,R,L]
I have an .htaccess that is working fine, I just need to add the page name before the parameters
Options -MultiViews
RewriteEngine On
RewriteBase /new/
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 [L]
# PRODUCT PAGE IS WORKING GREAT
# NEWS PAGE I AM TRYING TO SHOW THE PAGE NAME
RewriteCond %{THE_REQUEST} /morenews\?id=([^&\s]+)\s [NC]
RewriteRule . morenews/%1/? [R=301,L]
RewriteRule ^morenews/([^/]+)/$ morenews?id=$1 [L]
# THIS ISLEADING TO INTERNATL SERVER ERROR 500
#http://www.lebmotors.com/new/morenews/12/
# PRODUCT PAGE IS WORKING GREAT
# NEED TO SHOW moreprod before parametsr it is now just website new/1/2/3/4 i ned to make it website/new/moreprod/2/2/3/4
RewriteCond %{THE_REQUEST} /moreprod\?id=([^&\s]+)\s [NC]
RewriteRule . %1/? [R=301,L]
RewriteCond %{THE_REQUEST} /moreprod\?id=([^&\s]+)&topid=([^&\s]+)\s [NC]
RewriteRule . %1/%2/? [R=301,L]
RewriteCond %{THE_REQUEST} /moreprod\?id=([^&\s]+)&topid=([^&\s]+)&catid=([^&\s]+)\s [NC]
RewriteRule . %1/%2/%3/? [R=301,L]
RewriteCond %{THE_REQUEST} /moreprod\?id=([^&\s]+)&topid=([^&\s]+)&catid=([^&\s]+)&cagid=([^&\s]+)\s [NC]
RewriteRule . %1/%2/%3/%4/? [R=301,L]
RewriteRule ^([^/]+)/$ moreprod?id=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ moreprod?id=$1&topid=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ moreprod?id=$1&topid=$2&catid=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ moreprod?id=$1&topid=$2&catid=$3&cagid=$4 [L]
I am trying to show pagename before paramters
Resolved, just moved these lines to the bottom of .htaccess
RewriteEngine On
RewriteBase /new/
....
RewriteCond %{THE_REQUEST} /moreprod\.php\?id=([^&\s]+)\s [NC]
RewriteRule . moreprod/%1/? [R=301,L]
.....
RewriteCond %{THE_REQUEST} /moreprod\.php\?id=([^&\s]+)&topid=([^&\s]+)&catid=([^&\s]+)&cagid=([^&\s]+)\s [NC]
RewriteRule . moreprod/%1/%2/%3/%4/? [R=301,L]
RewriteRule ^moreprod/([^/]+)/$ moreprod.php?id=$1 [L]
.....
RewriteRule ^moreprod/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ moreprod.php?id=$1&topid=$2&catid=$3&cagid=$4 [L]
# THIS TO THE BOTTOM
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 [L]
I am using the code in below to redirect index.html and non-www version of URL to www. It also removes *.html extensions from the files. Now, I would like to add a trailing slash at the end of the files across all directories. Following are the examples of what I want to get:
www.mydomain.com.au/contact.html goes to www.mydomain.com.au/contact/
www.mydomain.com.au/contact goes to www.mydomain.com.au/contact/
www.mydomain.com.au/glass-replacement/Brisbane.html goes to
/glass-replacement/Brisbane/
and so forth...
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^.*\/index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]
RewriteCond %{http_host} ^mydomain.com.au$ [nc]
RewriteRule ^(.*)$ http://www.mydomain.com.au/$1 [r=301,nc,L]
RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.html)?(\?.*)?\ [NC]
RewriteRule ^(.+/)?index(\.html)?$ /%1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
Thanks for your help in advance
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^.*\/index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]
RewriteCond %{http_host} ^glassnow.com.au$ [nc]
RewriteRule ^(.*)$ http://www.glassnow.com.au/$1 [r=301,nc,L]
RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.html)?(\?.*)?\ [NC]
RewriteRule ^(.+/)?index(\.html)?$ /%1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.glassnow.com.au/$1/ [R=301,L]
I have the following problem:
http://www.mydomain.com/articles.php?artid=89
should become: http://www.mydomain.com/mykeyword
I dont mind if the id remain in the url...like: http://www.mydomain.com/89/mykeyword
I have the following .htaccess for the moment:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(mydomain\.com)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+news\.php [NC]
RewriteRule ^ mykeyword [R=301,L]
RewriteRule ^mykeyword/?$ news.php [L,NC]
The above part works like a charm.
Any help will be deeply appreciated.
Regards, Zoran
Append these rule at the bottom of your .htaccess:
RewriteEngine On
RewriteBase /turkexpo/
RewriteCond %{HTTP_HOST} ^(webone\.gr)$ [NC]
RewriteRule ^(.*)$ http://www.%1/turkexpo/$1 [R=302,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+turkexpo/news\.php [NC]
RewriteRule ^ mykeyword [R=302,L]
RewriteRule ^mykeyword/?$ news.php [L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+turkexpo/articles\.php\?artid=([0-9]+) [NC]
RewriteRule ^ %1/mykeyword? [R=302,L]
RewriteRule ^([0-9]+)/mykeyword/?$ articles.php?artid=$1 [L,NC,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+turkexpo/companies\.php\?fairid=([0-9]+)&ehallid=([0-9]+) [NC]
RewriteRule ^ companies/%1/%2? [R=302,L]
RewriteRule ^companies/([0-9]+)/([0-9]+)/?$ companies.php?fairid=$1&ehallid=$2 [L,NC,QSA]
This will forward a URI of /89/mykeyword to /articles.php?artid=89 and externally redirect the opposite of it.
Once you make sure it is working change R=302 to R=301.