Rewrite gives a 404 error - .htaccess

I need to rewrite an url from this
https://www.domain.tld/stand-carros-usados/?brand=2&model=&category=&fuel=&kms=&price=
to this
https://www.domain.tld/stand-carros-usados/alfa-romeo
and the code is this
RewriteCond %{REQUEST_URI} ^/stand-carros-usados [NC]
RewriteCond %{QUERY_STRING} ^brand=(.*)&model=(.*)&category=(.*)&fuel=(.*)&kms=(.*)&price=(.*)
RewriteRule ^(.*)$ https://www.domain.tld/stand-carros-usados/alfa-romeo? [L]
The url is changing the one i want but its gives me a 404 page?
Anyone knows why?
EDIT
This is the .htaccess file i have. A bit messi because who created didn´t care that much.
RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^pt/(.*)$ http://%{HTTP_HOST}/$1 [L,R]RewriteCond %
{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^viaturas(.*)$ https://%{HTTP_HOST}/stand-carros-usados$1 [R=301,L]
RewriteRule ^automoveis(.*)$ https://%{HTTP_HOST}/stand-carros-usados$1 [R=301,L]
RewriteRule ^print/([a-zA-Z0-9_-]+)?$ ajax/printcar.php?id=$1 [L]
RewriteRule ^process-order/([a-zA-Z0-9_-]+)?$ process-order.php?h=$1 [L]
RewriteRule ^([^\.]+)?$ index.php?section=$1 [L]
RewriteRule ^eng/([^\.]+)?$ index.php?section=$2 [L]
RewriteRule ^eng?$ index.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]

.htaccess REDIRECT_LOOP prevents PHP_SELF from executing functions properly

The following redirect is preventing a form with php_self from running functions on the same page properly.
I was having trouble removing the trailing slash for some reason on this server so I went this route. Found it interesting! The page refreshes but runs no code. Changing the page to prevent redirect goes back to working. Any thoughts?
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
DirectorySlash Off
RewriteRule ^bio$ /bio/index.php [L,E=LOOP:1]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^bio/$ /bio [R=301,L]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^bio/index.php$ /bio [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
You need to keep DirectorySlash off if you don't want / at the end of a directory:
DirectorySlash off
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule ^(.+?)\.php$ /$1 [L,R=301]
RewriteRule ^(.+?)/index$ /$1 [L,R=301]
# remove trailing slash
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=302,L]
# route to bio/index.php if request is /bio
RewriteRule ^bio$ bio/index.php [L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

.htaccess remove index.php remove www. force https

I understand this is not necessarily an EE specific question, but it involves the classic EE removal of index.php from the URL as well as a couple more specific rewrites. It’s also a lazy question, as I’ve tried a few rewrite rule combinations and there are errors… so, what I’m after is (please).
Remove www.
Force https://
Remove index.php
The important note, is that on an (mt) Media Temple server when testing on subdomains such as dev.domain.com the good old multiple redirects problem is kicking in. Ideally there’s a way for one .htaccess file to rule them all. Many thanks in advance.
# Enable Rewrite Engine
RewriteEngine On
RewriteBase /
# Force https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Remove the www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#Remove index.php
#strip index.php from the URL if that is all that is given
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://%{HTTP_HOST}/ [R=301,NS,L,QSA]
#strip index.php/* from the URL
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php/ [NC]
RewriteRule ^index\.php/(.+) http://%{HTTP_HOST}/$1 [R=301,L,QSA]
# EE
#rewrite all non-image/js/css urls back to index.php if they are not files or directories
RewriteCond $1 !^(images|templates|themes)/ [NC]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]
What worked for me (excluding the index.php part, only for removing WWW and forcing HTTPS), was this:
# Enable Rewrite Engine
RewriteEngine On
# Remove the www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Force https, specify http_host
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#-----the following part is untested, but should work to answer the full quesiton including the index.php part
#Remove index.php
#strip index.php from the URL if that is all that is given
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://%{HTTP_HOST}/ [R=301,NS,L,QSA]
#strip index.php/* from the URL
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php/ [NC]
RewriteRule ^index\.php/(.+) http://%{HTTP_HOST}/$1 [R=301,L,QSA]
# EE
#rewrite all non-image/js/css urls back to index.php if they are not files or directories
RewriteCond $1 !^(images|templates|themes)/ [NC]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/js/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/css/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/images/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/fonts/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/themes/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/inc/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/admin.php$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/index.php?$1 [QSA,L]
</IfModule>

non www to www, tailing slashes, non https to https and friendly URL's

Hi getting a little stuck with my .htaccess file rules.
I have set up non https redirects to https (this works):
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
I have set up non www to www (this works):
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I have set up so it always adds trailing slashes to urls (this adds a trailing slash to thank you.php):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
Redirects (not working):
RewriteRule ^thank-you/?$ thank-you.php [L]
RewriteRule ^thank-you.php$ http://%{HTTP_HOST}/thank-you/ [L]
What should happen is if someone goes to any address that not www or not http they are redirected to https and www.
whet the file thank-you.php is accessed it should redirect to /thank-you/
if someone accessed /thank-you it should redirect to /thank-you/
getting a little stuck as the URL re writings are not working but look like they should.
thanks in advance
full code:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
RewriteRule ^thank-you/?$ thank-you.php [L]
RewriteRule ^thank-you.php$ http://%{HTTP_HOST}/thank-you/ [L]
Now whats strange is this code redirects /thank-you.php to /thank-you/ but does not redirect /thank-you to /thank-you/ i think something is clashing:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
RewriteRule ^thank-you/?$ thank-you.php [L]
RewriteRule ^thank-you.php$ http://%{HTTP_HOST}/thank-you/ [L]
i got round my issue with the bellow:
htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
php:
function CheckUrl ($s) { // Get the current URL without the query string, with the initial slash
$myurl = preg_replace ('/\?.*$/', '', $_SERVER['REQUEST_URI']); //If it is not the same as the desired URL, then redirect
if ($myurl != "/$s") {Header ("Location: /$s", true, 301); exit;}
}
$producturl = "thank-you/";
CheckUrl ($producturl); //redirects the user if they are at the wrong place

Resources