.htaccess RewiteCond (QUERY_STRING) with 2 variable and rewrite properly - .htaccess

I need to rewrite my url from:
http://***.com/index.php?cat=VAR&page=1
to:
http://***.com/VAR/1
With 301 redirection.
I got this so far:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index.php\?
RewriteCond %{QUERY_STRING} ^cat=(.*)\&page=(.*)
RewriteRule . /%1/%2 [R=301]
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(files|admin)/
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&page=$2 [L]
But the first 3 rules doesn't seems to work at all. (I'm a beginner in htaccess)
How can i fix this problem? Thanks!
EDIT :
Thanks to Jassie, the solution:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index.php\?
RewriteCond %{QUERY_STRING} ^cat=(.*)\&page=(.*)
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&page=$2 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(files|admin)/
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&page=$2 [L]

change it to RewriteRule ^(.)/(.)$ /index.php?cat=$1&page=$2 [L,QSA] and try

Related

Htaccess www to non-www but keep the url intact

I think that this is rather simple, but I just can't wrap my head around it.
I have a domain like https://www.example.com
I want to rewrite it to https://example.com
That works. But I am also rewriting other urls like https://www.example.com/privacy
If I try to rewrite these too the user is always redirected to https://example.com/privacy.php
How do I prevent my server to show the actual filename instead of its rewritten url?
My code looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^checkout checkout.php [L]
RewriteRule ^imprint imprint.php [L]
RewriteRule ^privacy privacy.php [L]
RewriteRule ^terms terms.php [L]
RewriteRule ^allergy allergy.php [L]
RewriteRule ^nutrition nutrition.php [L]
RewriteRule ^order/([A-Z]*)?$ confirmation.php?lang=&id=$1 [L,QSA]
Any help is appreciated.
Regards
Have it this way:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(checkout|imprint|privacy|terms|allergy|nutrition)/?$ $1.php [L]
RewriteRule ^order/([a-z]+)/?$ confirmation.php?lang=&id=$1 [L,QSA,NC]
Make sure to test after clearing your browser cache. Problem appears to be due to your patterns not using end anchor.
I have combined your multiple similar rules into one rule.

cannot create clean url with .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]

.htaccess redirect all index.html to parent folder

I need to redirect all index.html like this
Original URL
www.example.com/lp/index.html
www.example.com/sytem/index.html
Desired URL
www.example.com/lp
www.example.com/sytem
I used the follwing, it redirects successfully but 404 page
RewriteEngine On
RewriteRule ^index\.html$ [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
You almost got it right, what you did looks like a copy paste error to me. Your first RewriteRule is missing a parameter - there should be a slash before [R=301,L]
Your htaccess should look like this:
RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
Reference:
http://dense13.com/blog/2012/10/29/removing-index-html-with-mod_rewrite-in-htaccess/
Place this code in your DOCUMENT_ROOT/.htaccess file:
DirectoryIndex index.html
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.html [NC]
RewriteRule ^(.*?)index\.html$ /$1 [L,R=301,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1/index\.html -f [NC]
RewriteRule ^(.+?)/?$ /$1/index.html [L]
Try putting this in your .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1/index\.html [PT,L]
PT and L are rewrite flags, for more details about them check http://httpd.apache.org/docs/2.2/rewrite/flags.html
why not redirect index.(anything) ? like index.php? index.xhtml?
RewriteEngine On
RewriteRule ^index\.(.*)$ / [R=301,L]
RewriteRule ^(.*)/index\.(.*)$ /$1/ [R=301,L]
or to base it from the initial URL request only (to avoid conflicts with other rewrite rules):
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.(.*) [NC]
RewriteRule ^(.*?)index\.(.*)$ /$1 [R=301,L]

What's wrong with this rewrite rule?

I have this .htaccess:
RewriteEngine On
RewriteRule ^!/(.*)$ path/to/a/file/$1 [L]
RewriteRule ^(.*)$ path/to/another/file/$1 [L]
I want urls in the form of www.website.com/!/this/ to be rewritten to path/to/a/file. Any URL that doesn't match that pattern should be rewritten to path/to/another/file/.
Here's what I've tried to far:
RewriteEngine On
RewriteRule ^!/(.*)$ path/to/a/file/$1 [L]
RewriteCond ...
RewriteRule ^(.*)$ path/to/another/file/$1 [L]
When using the above rewrite rule, I get a 500 - Internal Error.
How can I fix it?
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/!
RewriteRule ^!/([a-z0-9_\-\.]+) user/public/$1 [L]
RewriteCond %{REQUEST_URI} !^/!
RewriteRule ^([a-z0-9_\-\.]+)/([a-z0-9_\-\.]+)/?$ $1/controller/front.php/$2 [L]
RewriteCond %{REQUEST_URI} !^/!
RewriteRule ^([a-z0-9_\-]+)/([a-z0-9_\-]+)/([a-z0-9_\-]+)/?$ $1/controller/front.php/$2/$3 [L]

htaccess rule question

I have a htaccess which looks like this:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [QSA]
RewriteRule ^$ /naujausios [L,R]
And I'm not sure where to put this:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
for it to work properly - I tried a few modifications to the keys at the end, without success - I just end up with a 500 response. Thanks for your help!
I had conflicts wherever I put it, but the solution was to put the www bit rewrite at the very top just after RewriteEngine on

Resources