How would i properly structure this htaccess file as to correctly function in order and avoid infinite loops?
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
RewriteBase /
RewriteRule ^deviceToken/$ devicetoken.php [QSA,L]
RewriteRule ^register/$ register.php [QSA,L]
RewriteRule ^resetPassword/$ resetpassword.php [QSA,L]
RewriteRule ^deleteLink/$ deletelink.php [QSA,L]
RewriteRule ^getLinks/$ getlinks.php [QSA,L]
RewriteCond %{REQUEST_URI} !(\.gif)|(\.jpg)|(\.png)|(\.css)|(\.js)|(\.php)|(\.swf)|(\.xpi)|(\.ico)|(\.src)$
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule (.*)$ get.php?code=$1 [L]
It looks OK, have you tried it already?
This line is useless:
RewriteCond %{REQUEST_URI} ^(.*)$
Redirect domain first:
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
(added ,L)
Related
I'm really bad at htaccess setting and would like to have some help with it. I have a project inside a subfolder, and can be opened like this: www.mydomain.com/subfolder/. I put my project inside "subfolder". "subfolder" is not a subdomain, I just simply created a new folder in root folder.
I can open home page, but all other page like www.mydomain.com/subfolder/about, www.mydomain.com/subfolder/product have 500 internal server error page. I also can't access uploaded file in uploads folder, so URL like www.domain.com/subfolder/uploads/filename.jpg will return error 404. I'm pretty sure that there is no php error becase I also uploaded it in my other subdomain (different domain) and it works fine.
This is my htaccess setting:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} /(backend)
RewriteRule ^backend/assets/(.*)$ backend/web/assets/$1 [L]
RewriteRule ^backend/css/(.*)$ backend/web/css/$1 [L]
RewriteRule ^backend/font/(.*)$ backend/web/font/$1 [L]
RewriteRule ^backend/plugins/(.*)$ backend/web/plugins/$1 [L]
RewriteRule ^backend/images/(.*)$ backend/web/images/$1 [L]
RewriteRule ^backend/js/(.*)$ backend/web/js/$1 [L]
RewriteCond %{REQUEST_URI} !/backend/web/(assets|css|font|plugins|images|js)/
RewriteCond %{REQUEST_URI} /(backend)
RewriteRule ^.*$ backend/web/index.php [L]
RewriteCond %{REQUEST_URI} /(assets|css|js|img|fonts|uploads)
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
RewriteRule ^js/(.*)$ frontend/web/js/$1 [L]
RewriteRule ^img/(.*)$ frontend/web/img/$1 [L]
RewriteRule ^fonts/(.*)$ frontend/web/fonts/$1 [L]
RewriteRule ^uploads/(.*)$ uploads/$1 [L]
RewriteCond %{REQUEST_URI} !/(frontend|backend)/web/(assets|css|js|img|images|fonts|uploads|plugins|font)/
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php
Could someone help me?
After i check the script, i found some different in the code.
And i try to fixing some code like this.
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(backend)
RewriteRule ^backend/assets/(.*)$ backend/web/assets/$1 [L]
RewriteRule ^backend/css/(.*)$ backend/web/css/$1 [L]
RewriteRule ^backend/font/(.*)$ backend/web/font/$1 [L]
RewriteRule ^backend/img/(.*)$ backend/web/img/$1 [L]
RewriteRule ^backend/js/(.*)$ backend/web/js/$1 [L]
RewriteRule ^backend/plugins/(.*)$ backend/web/plugins/$1 [L]
RewriteCond %{REQUEST_URI} !/backend/web/(assets|css|font|img|js|plugins)/
RewriteCond %{REQUEST_URI} ^/(backend)
RewriteRule ^.*$ backend/web/index.php [L]
RewriteCond %{REQUEST_URI} ^/(assets|css|font|img|js|uploads)
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
RewriteRule ^font/(.*)$ frontend/web/font/$1 [L]
RewriteRule ^img/(.*)$ frontend/web/img/$1 [L]
RewriteRule ^js/(.*)$ frontend/web/js/$1 [L]
RewriteRule ^uploads/(.*)$ frontend/web/uploads/$1 [L]
RewriteCond %{REQUEST_URI} !/(frontend|backend)/web/(assets|css|font|img|js|uploads|plugins)/
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php [L]
There are some sign i add that is this sign ( ^ ).
And for uploading file (image, js, css, etc) i recommend you to upload to folder backend/web/somefolder or frontend/web/somefolder. Put beside those two place, for images, makes your images can not show on all over the place (in my experience).
And also, you can try to make the folder inside frontend/web/ or backend/web/ the same name, so that can makes you easier to remember (please see the code that i fix to see what i mean). I hope it helps for others.
If someone have the better solution, please share it here. Thanks.
I have the following existing .htaccess in place
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule home index.php [L]
RewriteRule ^([^/]*)/$ /index.php?TopicID=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?RollID=$1&FrameID=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
RedirectMatch 301 /cache(.*) //$1
ErrorDocument 404 http://www.example.com/error.php
I have many dynamically generated URLs which are being rewritten to this
www.example.com/abc_def_ghi/ OR
www.example.com/abc_def_ghi/abc_def_ghi/
and i'd like to end up with this
www.example.com/abc-def-ghi/ OR
www.example.com/abc-def-ghi/abc-def-ghi/
I've tried adding this to the end of my .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !.(html|php)($|?) [NC]
RewriteRule ^([^_]*)_+(.*)$ $1-$2 [E=underscores:Yes,N]
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) http://%{HTTP_HOST}/$1 [R=301,L]
but i'm getting a 500 error.
Can anyone help?
Rearrange your rules like this:
ErrorDocument 404 http://www.example.com/error.php
RewriteEngine on
RewriteRule ^cache(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# if there is only one underscore then repalce it by - and redirect
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=302]
# if there are more than one underscores then "repeatedly" repalce it by - and set env var
RewriteRule ^([^_]*)_([^_]*)_(.*) $1-$2-$3 [E=USCORE:1]
# if USCORE env var is set and there is no underscore left then redirect
RewriteCond %{ENV:REDIRECT_USCORE} =1
RewriteRule ^([^_]+)$ /$1 [L,R=302]
RewriteRule home index.php [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ /index.php?TopicID=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?RollID=$1&FrameID=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
I am facing problem with mod_rewrite ^(.+)$ /article.php?pname=$1 [L,QSA] is working fine but after that other RewriteRule dont work i dont know why ... here is my code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /article.php?pname=$1 [L,QSA]
RewriteRule ^category/(.*)$ /cat.php?name=$1 [QSA,L]
ErrorDocument 404 /index.php
</IfModule>
If i remove RewriteRule ^(.+)$ /article.php?pname=$1 [L,QSA] other RewriteRule works but it dont work with this, how can both work together ?
Please help
Keep your .htaccess like this:
ErrorDocument 404 /index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^category/(.*)$ cat.php?name=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ article.php?pname=$1 [L,QSA]
</IfModule>
Simple change order, from:
RewriteRule ^(.+)$ /article.php?pname=$1 [L,QSA]
RewriteRule ^category/(.*)$ /cat.php?name=$1 [QSA,L]
To:
RewriteRule ^category/(.*)$ /cat.php?name=$1 [QSA,L]
RewriteRule ^(.+)$ /article.php?pname=$1 [L,QSA]
Because (.+) will rewrite everithing including cat.php. So when you are trying to access cat.php?name=someName you will get /article.php?pname=cat.php%3Fname%3DsomeName.
You should do following:
RewriteCond $1 !^(cat\.php|someFolder|robots\.txt)
RewriteRule ^(.+)$ /article.php?pname=$1 [L,QSA]
Or even this:
RewriteCond %{DOCUMENT_ROOT} !-f
RewriteRule !(cat\.php|someFolder|robots\.txt)$ /article.php?pname=$1 [QSA,L]
robots.txt should not to be rewrited.
I want to write these two different rules in one file. How I have to do it?
RewriteEngine On
RewriteCond %{REQUEST_URI} /v0\.3/forum/
RewriteRule ^(.*)$ /v0.3/test.php?forum=$1 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_URI} /v0\.3/profile/
RewriteRule ^(.*)$ /v0.3/profiles.php?id=$1 [L,QSA]
Simply delete the second RewriteEngine On declaration
RewriteEngine On
RewriteCond %{REQUEST_URI} /v0\.3/forum/
RewriteRule ^(.*)$ /v0.3/test.php?forum=$1 [L,QSA]
RewriteCond %{REQUEST_URI} /v0\.3/profile/
RewriteRule ^(.*)$ /v0.3/profiles.php?id=$1 [L,QSA]
I want to force www in my urls. I'm having problems with writing. I know that this code will do the trick.
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1
But I want to put all in $_GET['page']
RewriteRule ^(.*)$ /index.php?page=$1 [L]
How should I put this together?
You need to add [QSA], which allows you to modify the query string of the URL:
RewriteRule ^(.*)$ /index.php?page=$1 [QSA,L]
this should do the job
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule .* index.php?page=$0 [L]