I am trying to redirect all incoming traffic to my site to index.php and put the remainder of the url in a path variable. I also have a index.html in my root directory and if the user accesses it directly I want them rerouted to the index.php script.
RewriteEngine On
RewriteBase /
RewriteCond $1 !^index\.html
RewriteCond $1 !^index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index\.html?$ / [NC,R,L]
RewriteRule ^(.*)$ index.php?path=$1 [L,QSA]
The rerouting seems to be working ok, but all my /api/index.php?route=xx&mode=xx seem to be getting rerouted too. Is there a way to exclude the api directory from the rewrite condition?
Could you please try following. This is checking if REQUEST_URI is not starting from /app/index.php then reroute everything to index.php along with passing parameters to it, couldn't test it as of now, will test it few mins or so.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/api/index\.php/?$ [NC]
RewriteRule ^(.*)$ index.php?path=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/api/index(\.html)/?$ [NC]
RewriteRule ^(.*)$ index.php [NC,L]
Related
When user enters url like
http://example.com/app/abcd123/
I want to show hime page from
http://example.com/app/index.php?param=abcd123
Without changing URL in browser.
I put .htaccess file inside app folder with code
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php [NC]
RewriteCond %{QUERY_STRING} ^param=(.*)
RewriteRule (.*) http://example.com/app/%1? [R=301,L]
You can use this code in /app/.htaccess:
RewriteEngine on
RewriteBase /app/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?param=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?param=$1 [L,QSA]
Try this .htaccess code. It should help you.
Make sure the rewrite base should be the form the root to your present directory.
RewriteBase /app/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?param=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
In this, if the user enters like http://example.com/app/qwerty the call will be processed like http://example.com/app/index.php?params=qwerty
This should be working, I tested it. Let me know if you face any troubles.
RewriteEngine On
RewriteBase /app
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php?param=$1 [L]
Check it there: http://htaccess.mwl.be/ or other online htaccess test service
I know there are plenty of other questions similar to this one, i tried followinf some of those, but with no luck.
I have a website called test.com and i need, when someone seraches for test.com to show the content in test.com/home, without having home in the uerl.
My htaccess file at the moment looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /(.*) home/$1 [L]
RewriteRule /home/(.*) /$1 [L,R=302]
RewriteOptions inherit
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
but when i type test.com it shows a list of folders, not the content inside home.
What am i doing wrong?
thanks
You can simplify your rules to this in root .htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^$ home/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!home/).+)$ home/$1 [L,NC]
Make sure to clear your browser cache before testing this.
I want all file or directory existed under up.example.com subdomain rejected and show 404 error.
Also I want all another request point to index.php.
I write this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^up\.(.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ - [R=404,L,NS]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]
When I test this code for http://up.example.com/test.jpg
If test.jpg file existed on root server, show me this error:
Not Found
The requested URL /test.jpg was not found on this server.
Apache Server at up.example.com Port 80
It's ok!
But when test.jpg not existed, give me this error:
Not Found
The requested URL /index.php was not found on this server.
Apache Server at up.example.com Port 80
Instead open index.php (index.php exist on root)
Why? and how can I fix it?
Actually the problem is that you write everything to index.php and that becomes a valid file in the request. When mod_rewrite runs next time first rule sends it to 404.
To overcome this error have your rules like this:
RewriteEngine On
# if it is valid file or directory except for index.php
# then send 404 to browser
RewriteCond %{HTTP_HOST} ^up\. [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php[?\s] [NC]
RewriteRule ^index\.php$ - [R=404,L,NC]
RewriteCond %{HTTP_HOST} ^up\. [NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !^index\.php$ - [R=404,L,NC]
# send all non-file/directories to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Try this one
RewriteEngine On
RewriteCond %{HTTP_HOST} ^up\.(.+)$ [NC] # remove if it is not related to subdomain
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]
RewriteCond %{HTTP_HOST} ^up\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ - [R=404,L,NS]
I want to make a htaccess RewriteRule with a RewriteCond
What I have so far:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1
I want all requests sent to sql.site.ro to be served from the /ro-site-sql directory.
So if the URI is sql.site.ro/index.php the server will go to /ro-site-sql/index.php, or if URI is sql.site.ro/images/small/9954.png the server will go to /ro-site-sql/images/small/9954.png.
The problem is that if I make
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteRule ^(.*)$ ro-site-sql/$1
I get an Internal Server Error, and if I make
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1
I get Array ( [x] => ro-site-sql/index.php ) inside of /ro-site-sql/index.php. Should it not be just index.php, without the ro-site-sql/ part?
Thanks!
RewriteCond %{HTTP_HOST} ^sql.site.ro$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 ^(\w+/)+\w+\.\w+$
RewriteRule ^(.*)$ ro-site-sql/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sql.site.ro$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(\w+/)+\w+\.\w+$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1 [L,QSA]
This should work to identify actual files and access those and pass anything else to index.php.
The rewrite engine loops over .htaccess files until no more substitutions occur. You need to ensure that this loop stops after the first replacement. There are various ways to do this. Perhaps the easiest to understand is:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteCond $1 !index.php$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1 [L,QSA]
Add the [L] flag because this is the last rule on this cycle and [QSA] if you also have other parameters sometimes.
I know the topic "How to force HTTPS + WWW" is often discussed and solved, and in general it works for me.
But as I now got a specific predefined .htaccess from CakePHP I do not know how to include it.
.htaccess for CakePHP:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
If I put the normal code for HTTPS/WWW Forcing in front or backwards to that code, it does not work properly because all requests are set to root directory and not to e.g. /contact.
Normally I am using:
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteRule ^ https://www.mydomain.com%{REQUEST_URI} [R=301]
But you can not just include that above...
Could anybody please help me including HTTPS/WWW Forcing in the above .htaccess?
The generic way is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
No need to hard-code domain name. Also request will not redirect to root when switching from http to https
Thanks LazyOne, this maybe working, but for me it often ended up in "mydomain.com/redirect:/app/webroot/index.php" which was really strange. But maybe this is due to the "{REQUEST_URI}" because I had to change my
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
to
RewriteRule ^(.*)$ index.php [QSA,L]
due to strange problems with redirect (no idea what happend, CakePHP suddenly requestet a "Redirect:Controller" as also described here http://groups.google.com/group/croogo/browse_thread/thread/55539dabfd0191fd?pli=1 - any idea about this?).
It is now working with this code:
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) https://www.mydomain.com/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.mydomain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
This is how it should be:
RewriteEngine On
# force https and www.
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteRule ^ https://www.mydomain.com%{REQUEST_URI} [R=301,L]
# route all requests for non-existing resources to CakePHP
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
another way might be to force SSL using a component ? http://bakery.cakephp.org/articles/lemon/2008/07/07/component-for-forcing-a-secure-connection