How to write htaccess in this case - .htaccess

this is my .htaccess
RewriteEngine On
RewriteCond $1 !^(index\.php|public)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(acp|acp/)$ /acp/home.html [L,R=301]
RewriteRule ^(.*).html$ index.php/$1 [QSA,L]
But when i upload html file into root folder (example: ads.html) and run http://domain.com/ads.html it's not work, how to fix it

Conditions only work for the rule they appear before. Your conditions work for the rule that rewrites acp to acp/home.html. You want to move the conditions directly before the second rule. You would end up with:
RewriteEngine On
RewriteRule ^(acp|acp/)$ /acp/home.html [L,R=301]
RewriteCond $1 !^(index\.php|public)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*).html$ index.php/$1 [QSA,L]

Related

.htaccess multiple rewrite rules

I currently have a .htaccess rewrite rule that will redirect all urls containing /ws to the /ws/index.php eg. www.domain.com/ws/controller/function
RewriteEngine on
RewriteBase /ws/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
i'm looking to add another redirect, just the same, for all request that contain /email/ so www.domain.com/email/controller/function will redirect to the /email/index.php.
my full .htaccess looks like the code below but it seems the /email/ never gets called.
RewriteEngine on
RewriteBase /ws/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
RewriteEngine on
RewriteBase /email/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
I've tried adding ^/email to the RewriteRule conditional but to no avail.
Any help would be appreciated.
Thanks
You can use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^ws/(.*)$ ws/index.php?url=$1 [NC,L,QSA]
RewriteRule ^email/(.*)$ email/index.php?url=$1 [NC,L,QSA]

.htaccess call PHP file with parameters

Sorry if the title of the question is not clear, please read below for details;
I have this .htaccess:
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/home/.*$ index.php [QSA]
One of its functions is to redirect the url from this example:
www.example.com/user.php?user=david
to be:
www.example.com/david
Now, I have a file friends.php which function is to show friends of a particular user.
So the link that I wanted is like this:
www.example.com/david/friends
How can I achieve this?
You have to basically do the same as you did for user. In fact, the rule does not even conflict, because the user rule cannot contain a / character.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/friends$ friends.php?user=$1 [L]

404 page not found in YAMS

I would like some help here. I am using MODx with YAMS module which is for multilingual. I follow the installation and setup document from YAMS but I still get 404 page not found.
I would like to get:
localhost/sub/en/index.php?id=1
localhost/sub/fr/index.php?id=1
localhost/sub/th/index.php?id=1
the original link is
localhost/sub/index.php?id=1
here is rewrite rule in htaccess file. I got the rewrite rule from YAMS in "Server Config" tab.
# Friendly URLs
RewriteEngine On
RewriteBase /sub/
# Fix Apache internal dummy connections from breaking [(site_url)] cache
RewriteCond %{HTTP_USER_AGENT} ^.*internal\ dummy\ connection.*$ [NC]
RewriteRule .* - [F,L]
# Exclude /assets and /manager directories from rewrite rules
RewriteRule ^(manager|assets) - [L]
# Redirect from mydomain.com/rootname to mydomain.com/rootname/
RewriteRule ^en$ en/ [R=301,L]
RewriteRule ^fr$ fr/ [R=301,L]
RewriteRule ^th$ th/ [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^th/(.*)$ index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fr/(.*)$ index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/(.*)$ index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I am trying to search all the solutions everywhere. still no luck. please suggest or point me what i do wrong?
Thanks in advance
If you are getting links like localhost/en/index.php?id=1 in the frontend You have to fill the "MODx Subdirectory" field with "sub". You will find it in Modules->Yams->Other Params
Actually the problem is .htaccess file. I copys all .htaccess from YAMS and replaces the whole original .htaccess file. To solve my problem, I copy only friendly URL part and replace only this part in original .htaccess file.
here what I copy from YAMS and replace in my .htaccess file:
# Redirect from mydomain.com/rootname to mydomain.com/rootname/
RewriteRule ^en$ en/ [R=301,L]
RewriteRule ^fr$ fr/ [R=301,L]
RewriteRule ^th$ th/ [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^th/(.*)$ index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fr/(.*)$ index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/(.*)$ index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Many thanks for suggestions.

.htaccess doesn't work properly

i have .htaccess file which includes below code.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L]
RewriteRule ^imagefactory/(.*)$ imagefactory/index.php?q=$1 [QSA]
i want .htaccess file which fulfill below requirements.
if
1) url/admin then go to the admin folder and choose it's .htaccess file
2) url/imagefactory then go to the imagefactory folder and choose it's file called index.php
3) url/ then it choose root directory's index.php file
Try This
RewriteEngine on
RewriteRule ^imagefactory/(.*)$ imagefactory/index.php?q=$1 [QSA]
RewriteRule ^admin/.$ - [PT]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1
You should place the last rule 1 position up. This rule RewriteRule ^(.*)$ index.php?q=$1 [L] will match anything, and the [L] attribute there means not other rules will be evaluated when it matches. If you reorder it like below it should work.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^imagefactory/(.*)$ imagefactory/index.php?q=$1 [QSA]
RewriteRule ^(.*)$ index.php?q=$1
The .htaccess file is read in sequence. The RewriteCond are only meant for one rule.
Therefore all you need is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^imagefactory/(.*)$ imagefactory/index.php?q=$1 [QSA, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/admin/(.*)$
RewriteRule ^(.*)$ index.php?q=$1 [QSA, L]
The second block is a catch all so that if its not from directory admin, use the default root index.php.

Are conditionals in ModRewrite possible?

I have a simple piece of ModRewrite that channels everything to index.php if it's not an existing file or directory.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Now I want to add a exception when the domain contains certain strings, but I don't know how to to add this. I was thinking of adding the following.
RewriteCond %{HTTP_HOST} ^(aanmelding|keyclamps|probouw)
RewriteRule ^(.*)$ /project.php/$1 [L]
UPDATE, I found a partial solution
If I put it like this:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If not an old project
RewriteCond %{HTTP_HOST} !^(aanmelding|keyclamps|probouw)
# forward it to index.php
RewriteRule ^(.*)$ /index.php/$1 [L]
# Else
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# forward it to project.php
RewriteRule ^(.*)$ /project.php/$1 [L]
It works, but with a bug. Because index.php exists, the second part of the conditional still goes to index.php instead of project.php when a plain domain is called like http://probouw.localhost/
Any ideas?
For anyone that might need this, the solution was:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If not an old project
RewriteCond %{HTTP_HOST} !^(aanmelding|keyclamps|probouw)
# forward it to index.php
RewriteRule ^(.*)$ /index.php/$1 [L]
# Else
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} =/
# forward it to project.php
RewriteRule ^(.*)$ /project.php/$1 [L]
Perhaps using OR would help (NC=NotCase sensitive by the way)?
RewriteCond %{HTTP_HOST} ^.*aanmelding.*$ [NC,OR]
RewriteCond %{HTTP_HOST} ^.*keyclamps.*$ [NC,OR]
RewriteCond %{HTTP_HOST} ^.*probouw.*$ [NC]
In reference to your partial solution, if you change the last part does this work?
# forward it to project.php
RewriteRule ^/?$ /project.php [L]
RewriteRule ^(.*?)$ /project.php/$1 [L]

Resources