简体   繁体   中英

What should I do to make the pretty URLs using .htaccess for my specific scenario?

Here's my full case:

  1. mvcframework directory inside /var/www/html/ dir.
  2. mvcframework contains a public dir with index.php acting as a frontloader.
  3. I am creating a .htaccess file inside public directory with following code:
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule  ^(.+)$ index.php?$1 [QSA,L]
</IfModule>
  1. I am accessing this through virtual host url mvcframe.local that is setup to direct at public folder:
<VirtualHost 127.0.0.3:80>
    ServerName mvcframe.local
    DocumentRoot /var/www/html/mvcframework/public

    <Directory /var/www/html/mvcframework>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>
    ErrorLog /var/www/html/mvcframework/mvc-error.log
    CustomLog /var/www/html/mvcframework/mvc-access.log combined
</VirtualHost>

  1. When I access http://mvcframe.local or http://mvcframe.local/ it outputs index.php content inside public folder index.php as it should.

  2. When I access http://mvcframe.local/?posts/hello it outputs:

Requested URL = posts/hello
  1. But when I access http://mvcframe.local/posts/hello removing ?, it gives:
Not Found
The requested URL was not found on this server.

Apache/2.4.41 (Ubuntu) Server at mvcframe.local Port 80

I am trying to figure out the solution searching for 2 hours and still haven't got the solution for it.

Alright, so after messing around and reading .htaccess tutorials for hours, I finally managed to understand my mistake. First of all, here is the video which helped me understand my mistake.

Here's my solution:

<IfModule mod_rewrite.c>
  Options -Multiviews
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule  ^(.+)$ index.php?$1 [QSA,L]
</IfModule>

First, I am checking if mod_rewrite is enabled. Then providing some options. Then turning on the RewriteEngine which is neccessary for enabling rewriting rules. Then giving the Rewrite Conditions for [If no directory] and [If not file] for whatever query is being supplied, for which I rewrite the rule basically telling it that check for all string that doesn't end with extension ".", simply direct them all to index.php?$1.

Basically anything that has mvcframe.local/something_here/some_here/... will become automatically mvcframe.local/index.php?something_here/some_here

For my setup, index.php is the frontloader so everything must go through this file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM