Htaccess Yii 2 Configuration - apache

I am working on a Yii2 Project and have set it up on Wamp Localhost. My Wamp I have set up an alias:
Alias /project "c:/Users/Shaun/Desktop/projects/project/web/"
<Directory "c:/Users/Shaun/Desktop/projects/project/web/">
AllowOverride all
</Directory>
In a basic app I have placed the htaccess file into the web directory (where it is currently pointing to)
project/web/.htaccess
project/web/index.php
project/web/index-test.php
mod_rewrite is definitely enabled I have checked. I can navigate to my root without a problem:
This is my htaccess file as demonstrated on Yii2 docs.
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
http://localhost/project/
When I navigate to url above I hit my SiteController Index action fine. But when I try to go to for example:
http://localhost/project/site/index
It states:
The requested URL /Users/Shaun/Desktop/projects/project/web/index.php was not found on this server.
But it is definitely there?! If you see my web.php configuration as a component I have enabled:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
]
Still can't find it. But if I do this it shows the routes fine:
http://localhost/project/index.php/site/index
So frustrating as I don't want this. What's the deal here?
Thanks,

Because you are working on the virtual directory (e.g. Alias /site/ "/dir/"), you must use
RewriteBase /site/
in your apache configuration file.

The structure of your Project seems a bit unsual.
Is there another project folder in the folder web ?
If the answer is yes, you have to put the .htaccess file in this project folder.
But if the project folder contains web, it's quite unsual. But also in that case, the .htaccess file has to be in the project folder.

Set AllowOverride All in httpd.conf or your vhost conf.
This is mandatory to work with pretty URLs.

You have to change three files.
basic > .htaccess
basic > web > .htaccess
basic > config > web.php
Use the code given in the below url.
http://www.codematrics.com/htaccess-in-yii2-basic-app/
This will help you.

Related

How to config the default root index in hosting server

I wanted to put the index.php file of my website in a public file on my server, but consequently, when I tap the url, it is not directing me to the index, but to the folder which contains all of the folder of my website.
I try to put a .htaccess file in the root of the folder containing my website. It worked with my local virtual host wampserver, but not with the real host (where I put files with filezila). I show you the line code I wrote in .htaccess, which worked with wampserver, but not with the actual used server for internet ::
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ public/index.php [QSA,L]
I try also to put the absolute path but same thing. Does someone have an idea about what I have to do to config the index path of my server ? (for a subdomain) ?
I found the solution. I had to remove the first line:
Options +FollowSymlinks
My server already had it activated and didn't support the instruction twice.

url rewriting "double path for same file"

I have an issue with my htaccess file but I dont understand where the mistake comes from:
Ex:
I have the following file in my folder
contact.php
I did a basic rewrite like that
RewriteRule ^about/$ contact.php [L]
so now my contact page is accessible from http://localhost/project/about/
the problem is that page is also accessible from
http://localhost/project/contact/
Why is this happening?
How can I disallow that?
Thanks for all your answers!
That is due to enabling of option MultiViews. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
To disallow this you can add this line on top of your .htaccess:
Options -MultiViews

How To Use Htacess for www.example.com/type.php?user=name

I want to redirect www.example.com/type/name to www.example.com/type.php?user=name
{
RewriteRule ^([a-zA-Z0-9_-]+)/type$ type.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/type/$ type.php?key=$1
}
Please help me about this.
Try adding this to the .htaccess file in your web document root folder (often public_html or htdocs):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^type/([^/]+)/? type.php?key=$1 [L]
This assumes that mod_rewrite is both installed and activated for .htaccess files. If you are not sure, to check if mod_rewrite is even installed, look at the list of installed modules in the output of phpinfo(); By default, mod_rewrite is not enabled for .htaccess files. If you are managing your own server, open httpd.conf and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All
try this , hope it helps :
RewriteBase /www.example.com
RewriteEngine On
RewriteRule ^type/(.*)/$ /type.php?user=$1
#OR
RewriteRule ^(.*)/(.*)/$ /$1.php?user=$2
PS : The first one works only for the url : www.example.com/type/value ,the second one works for any page in your site ex: www.example.com/anypage/value

Apache problems on AWS EC2 - URL path interpreted as sub-directory

I'm trying to migrate a website from cPanel based hosting to AWS EC2 and am having problems with URL Paths being interpreted as sub-directories.
for example www.mysite.com/page/page-name is giving the error in the apache error log /var/www/html/page/page-name not found.
I've hardly changed the Apache config file at all.
As you can see above the site is running in /var/www/http, I'm not using any vhosts
The site is written using the Yii framework on PHP
The home page display OK, so php and Yii are OK.
I have the normal htaccess file in the web root:-
RewriteEngine on
AddDefaultCharset utf-8
DirectorySlash Off
RewriteBase /
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
and in Yii configuration I'm using the usual urlManager config:
'urlFormat' => 'path',
'showScriptName' => false,
Is there something I should be adding to httpd.conf?
You may need to enable AllowOverrides in order for your .htaccess to take effect.
Look in your httpd.conf file and find the following line
AllowOverride None
Change this to
AllowOverride All
There may be more than one AllowOverride directive. The one you want to change is the one within the Directory stanza for /var/www/html.

Rewrite url rules in .htaccess inside a directory

I'm trying to use rewrite url rules using a .htaccess file. This seems to work fine in my main .htaccess file (only works for php pages which exist in same directory), but when I want to use another .htaccess for php file inside a subdirectory the rules don't apply at all and I get a 404 error page.
The file I want to point to is /news/story.php. story.php requests an integer variable called 'article' to fetch the article from database and display it.
So basically what I want to do is replace http://www.mydomain.com/news/story.php?article=1
with http://www.mydomain.com/news/story/article/1/
I'm using the following rules in the .htaccess file which is inside the 'news' directory.
# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymLinks
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule story/article/(.*)/ story.php?article=$1
RewriteRule story/article/(.*) story.php?article=$1
Thanks for your help
RewriteRule story/article/(.*)/ news/story.php?article=$1
RewriteRule story/article/(.*) news/story.php?article=$1