cannot create clean url with .htaccess - .htaccess

I provided this .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.lebmotors.com/new/
RewriteRule (.*) http://www.lebmotors.com/new/$1 [R=301,L]
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ moreprod.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ moreprod.php?id=$1
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ moreprod.php?id=$1&topid=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ moreprod.php?id=$1&topid=$2
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ moreprod.php?id=$1&topid=$2&catid=$3
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ moreprod.php?id=$1&topid=$2&catid=$3
Still it does not work
I get: http://www.lebmotors.com/new/moreprod.php?id=107&topid=4&catid=8
What AM I doing wrong
Many Thanks

First of all, you don't need to repeat RewriteEngine On directive.
Then, when you add your rules, you still can access your pages via moreprod.php (but you can also access it from new clean url).
Here's how your htaccess should look like, assuming moreprod.php is also in new folder
Options -MultiViews
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]+)\s [NC]
RewriteRule . moreprod/%1/%2/? [R=301,L]
RewriteCond %{THE_REQUEST} /moreprod\.php\?id=([^&\s]+)&topid=([^&\s]+)&catid=([^&\s]+)\s [NC]
RewriteRule . moreprod/%1/%2/%3/? [R=301,L]
RewriteCond %{THE_REQUEST} /morenews\.php\?id=([^&\s]+)\s [NC]
RewriteRule . morenews/%1/? [R=301,L]
RewriteRule ^morenews/([^/]+)/$ morenews.php?id=$1 [L]
RewriteRule ^moreprod/([^/]+)/$ moreprod.php?id=$1 [L]
RewriteRule ^moreprod/([^/]+)/([^/]+)/$ moreprod.php?id=$1&topid=$2 [L]
RewriteRule ^moreprod/([^/]+)/([^/]+)/([^/]+)/$ moreprod.php?id=$1&topid=$2&catid=$3 [L]
With this code, if you try (for example) to reach http://domain.com/new/moreprod.php?id=123&topid=456&catid=789 then you'll redirect to http://domain.com/new/moreprod/123/456/789/
Conclusion
You can now access your pages via clean urls like
http://domain.com/new/moreprod/123/ (id=123)
http://domain.com/new/moreprod/123/456/ (id=123 and topid=456)
http://domain.com/new/moreprod/123/456/789/ (id=123 and topid=456 and catid=789)

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]

Related

htaccess rule issue in redirecting the url

I want to redirect all below pattern url to before "?" url
i.e
Xyz.com/worksheet/rebus/?random=testing11
Xyz.com/worksheet/rebus/?abc
Xyz.com/worksheet/rebus/?l=1
should be redirected to
Xyz.com/worksheet/rebus/
I tried many things but not able to succeed. How can i do using .htaccess
Rules tried
RewriteBase /worksheet/
RewriteRule ^rebus/?$ /worksheet/rebus/ [L,R=301]
Overall
RewriteEngine On
RewriteBase /worksheet/
RewriteCond %{THE_REQUEST} \s/(rebus)/\?(\S+)\s [NC]
RewriteRule ^ /worksheet/%1/ [R=301,L]
#RewriteRule ^rebus/?$ /worksheet/rebus [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]
Have it this way:
RewriteEngine On
RewriteBase /worksheet/
# \?\S matches at least one character after ?
RewriteCond %{THE_REQUEST} \s/(worksheet/rebus)/\?\S [NC]
RewriteRule ^ /%1/? [R=301,L]
RewriteRule ^rebus/?$ /worksheet/rebus? [L,R=301,NC]
# skip all rules for real files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]

Convert display language to subfolder with htaccess

For a website I have locally, I need to set the display language as if it were a subfolder, using an htaccess file.
For example, if I choose to display the site in German, instead of displaying it like this => local_ip/test/index.php?lang=de, I would like it to be displayed like this => local_ip/test/de/ .
Also, if I type in the url without setting the language, I would like htaccess to automatically redirect to the German language, from so => local_ip/test/tour/mountain/ to so => local_ip/test/de/tour/mountain .
My current htaccess file is structured as follows:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{QUERY_STRING} ^t=(.+?)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&t=%1 [NC,L]
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&p=%1 [NC,L]
RewriteCond %{QUERY_STRING} ^t=(.+?)&p=([0-9]+)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&t=%1&p=%2 [NC,L]
RewriteCond %{QUERY_STRING} ^c=true
RewriteRule ^city/(.+?)/?$ city.php?url=$1&c=true [NC,L]
RewriteRule ^city/(.+?)$ city.php?url=$1 [NC,L]
RewriteRule ^tour/(.+?)$ tour.php?url=$1 [NC,L]
SOLVED
After several attempts and after reading several support requests posted in this community, I found a way to make the system recognise all the other rules as well. All I had to do was put them before the last rule provided by the user who supported me. Here is the final code.
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !/de/ [NC]
RewriteRule (.*) /test/de/$1 [R,L]
RewriteRule ^de/$ /test/index.php?lang=de [L]
RewriteCond %{QUERY_STRING} ^t=(.+?)
RewriteRule city/(.+?)/?$ city.php?url=$1&t=%1&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule city/(.+?)/?$ city.php?url=$1&p=%1&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^t=(.+?)&p=([0-9]+)
RewriteRule city/(.+?)/?$ city.php?url=$1&t=%1&p=%2&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^c=true
RewriteRule city/(.+?)/?$ city.php?url=$1&c=true&lang=de [NC,L]
RewriteRule city/(.+?)$ city.php?url=$1&lang=de [NC,L]
RewriteRule tour/(.+?)$ tour.php?url=$1&lang=de [NC,L]
RewriteRule ^de/(.*)$ /test/$1 [L]
You can use the following :
RewriteEngine on
#redirect URLs to include /de
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/de/ [NC]
RewriteRule (.*) /test/de/$1 [R,L]
# /test/index.php?lang=de to /test/de
RewriteRule ^de/$ /test/index.php?lang=de [END]
RewriteRule ^de/(.+)$ /test/$1 [END]

Need to show my page prior to parameters in my .htaccess

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]

Removing html extentions and add trailing slash to files on all directories without affecting internal urls

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]

rewrite dynamic url

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.

Resources