Apache Htaccess forwarding sub folders - apache

I want to create a custom rule for htaccess so that all the URL's requested in the home folder such as localhost/asfsd will redirect to localhost/index.php?url=asfsd but if the user enters the following url localhost/asfsd- he must then be forwarded to localhost/info.php?url=asfsd . Here is my current .htaccess configuration :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?url=$1 [L,QSA]

You can try these 2 rules:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)-$ /info.php?url=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?url=$1 [L,QSA]

Try this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\-$ info.php?url=$1 [L,QSA]
RewriteRule ^(.*) index.php?url=$1 [L,QSA]

Related

.htaccess multiple parameters is not working

I have a problem with htaccess rule because is not working. I already try to find any solution on google but still have not found yet.
The code is:
RewriteEngine ON
RewriteBase /deshop/
RewriteCond %{REQUEST_URI} ^/deshop/admin(.+)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ admin/index.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ index.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^\s]+)$ index.php?p=$1&b=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([0-9]+)$ index.php?p=$1&id=$2 [L]
The urls are working normally:
http://localhost/deshop/index.php to http://localhost/deshop/ -> home page from folder
http://localhost/deshop/admin/index.php to http://localhost/deshop/admin/ -> admin page from subfolder
http://localhost/deshop/index.php?p=tshirts to http://localhost/deshop/tshirts/
http://localhost/deshop/index.php?p=brand&b=John%20Player to http://localhost/deshop/brand/John-Player/
So that happen, last rule does not work.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([0-9]+)$ index.php?p=$1&id=$2 [L]
But last rule has worked yet. I really difficult. I want get url with parameters so look work :
http://localhost/deshop/index.php?p=single&id=0005 to http://localhost/deshop/single/0005
Or, what am I wrong? Any solution? Can you help me? Thanks a lot. By the way, my english is not good. I'm sorry!
Finally I have solved it.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/miistore/miiadmin$
RewriteRule ^(.+)$ miiadmin/index.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
RewriteRule ^/?single/([^/d]+)/?$ index.php?p=single&id=$1 [L,QSA]
RewriteRule ^/?brand/([^\s]+)/?$ index.php?p=brand&b=$1 [L,QSA]

Apache2 rewrite - conditional

I have the following .htaccess file in my project's web root:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /repos/eamorr.co.uk/react_www/index.html [L,QSA]
The above works.
When I navigate to:
http://localhost/repos/eamorr.co.uk/react_www
I see my homepage.
And when I navigate to:
http://localhost/repos/eamorr.co.uk/react_www/contact-us
I see the "contact-us" route of my homepage (my webpage is written in React).
OK. Great.
Now, some users are on Linux:
When they checkout the code, they see the site at a slightly different url:
http://localhost/_eamorr/develop/eamorr.co.uk/react_www/
and
http://localhost/_eamorr/develop/eamorr.co.uk/react_www/contact-us
Regarding this line of the .htaccess file:
RewriteRule . /repos/eamorr.co.uk/react_www/index.html [L,QSA]
Instead of . is there some conditional that I can put in to serve index.html from a different path?
e.g.
RewriteRule {{condition1???}} /repos/eamorr.co.uk/react_www/index.html [L,QSA]
or (different condition = different path):
RewriteRule {{condition1???}} /_eamorr/develop/eamorr.co.uk/react_www/index.html [L,QSA]
I'm afraid my Apache knowledge isn't up-to-speed to do this quickly and I've spent hours at this!
=================
Solution:
Use a cascading htaccess rewrite rule setup as follows:
RewriteEngine On
#RewriteBase /
#Linux localhost:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*repos/eamorr.co.uk/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /repos/eamorr.co.uk/react_www/index.html [L,QSA]
#Mac localhost:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*_eamorr/eamorr.co.uk/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /_eamorr/eamorr.co.uk/react_www/index.html [L,QSA]
#develop .co.uk
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*gitWebFlow/eamorr.co.uk/develop/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /gitWebFlow/eamorr.co.uk/develop/react_www/index.html [L,QSA]
#master .co.uk
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*gitWebFlow/eamorr.co.uk/master/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /gitWebFlow/eamorr.co.uk/master/react_www/index.html [L,QSA]
#default
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L,QSA]
This is good enough for my needs right now.
If react_www is your project's root, and you're not referencing assets above that directory (publicly, via URLs), that's where the .htaccess should be. Then it would become
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . index.html [END]
You shouldn't need RewriteBase since you're not doing server-side external redirects, and just want virtual URLs below that directory to all serve the same file.

WAMP nice URL's redirect to direct file

Hello when I trying nice url with htaccess it's redirecting to file, so when I type www.page.com/mainpage it's redirect to www.page.com/mainpage.php and not to www.page.com/index.php?arg1=mainpage
my .htaccess looks like
RewriteEngine On
RewriteBase /
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ?arg1=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^([^/]+)/([^/]+)?/?$ ?arg1=$1&arg2=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?/?$ ?arg1=$1&arg2=$2&arg3=$3 [L,QSA]
You probably have the MultiViews enabled. Also, your rewrites are not referencing index.php anywhere. Try the following:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?arg1=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^([^/]+)/([^/]+)?/?$ index.php?arg1=$1&arg2=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?/?$ index.php?arg1=$1&arg2=$2&arg3=$3 [L,QSA]

htaccess rewrite 2 rule Simultaneously

I hava an htaccess file.it pass url to index.php
<!-- language-all: lang-html -->
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
now i wanna to remove "www" befor my domain name and also keep my previous RewriteRule .
How to do it؟
thanks
You can have a new rule for www removel before your current rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

.htaccess RewriteRule country codes and urls

I am using the following .htaccess code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ca/(.+)$ /index.php?p=$1&c=ca [L,QSA]
RewriteRule ^fr/(.+)$ /index.php?p=$1&c=fr [L,QSA]
RewriteRule ^(.+)$ /index.php?p=$1 [L,QSA]
In order to achieve the following effect:
http://xyz.com/ca/test -> http://xyz.com/index.php?p=test&c=ca
http://xyz.com/fr/test -> http://xyz.com/index.php?p=test&c=fr
http://xyz.com/test -> http://xyz.com/index.php?p=test
But it is failing with a server error. Any ideas on how to fix it?
Thanks
RewriteCond conditions only apply to the RewriteRule that immediately follows the condition(s). Your last 2 rules don't have any conditions on them and the rules are looping. Just add the 2 conditions in front of the last 2 rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ca/(.+)$ /index.php?p=$1&c=ca [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fr/(.+)$ /index.php?p=$1&c=fr [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?p=$1 [L,QSA]