phpstudy laravel设置伪静态apache|Nginx

3 篇文章 0 订阅

phpstudy laravel设置伪静态apache|Nginx

伪静态是网页开发逃不过的话题,操作起来并不是很难,但就是每次配的时候都会出现忘记了上次是咋弄的,因此一顿查,一顿看,终于配好了。等下次再需要的时候又忘记上次怎么配置的,重复往复,这大概就是程序员的小烦恼吧,有没有同感呢?因此仔细完整的记录下来是非常有必要。
下面就来看下如何配置伪静态。

前置条件

本次的配置环境:

  1. laravel
  2. phpstudy
  3. apache或者nginx

apache

打开apache路由重写的功能

该功能是apache内部功能,再apache配置文件 httpd.conf 中 ,将下面这句话前面的#去掉表示激活重写功能

LoadModule rewrite_module modules/mod_rewrite.so

设置项目.htaccess文件

laravel项目的起始文件是 public/index.php 因此该文件所在目录下会有这么一个文件 .htaccess ,想必大家都是非常清除的,将下面的代码写入文件中

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

phpstudy中配置网站

配置好路由重写后,你可以使用127.0.0.1地址直接访问,也可以使用phpstudy中的 “网站” 功能。如下添加
在这里插入图片描述
此时直接访问项目即可完成伪静态

nginx

配置方式和上面的apache一摸一样,唯一区别在于项目中nginx对应的文件名为nginx.htaccess。向该文件中添加下面内容即可。

# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /exploit/index.php?$query_string;
if (!-e $request_filename) {
    rewrite  ^(.*)$  /index.php?s=$1  last;
    break;
}

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,要使用 Laravel 伪静态,需要在 ApacheNginx 中配置伪静态规则。以下是在 Apache 中配置的示例: 1. 打开 Apache 的配置文件 `httpd.conf`,取消注释 `mod_rewrite` 模块: ``` LoadModule rewrite_module modules/mod_rewrite.so ``` 2. 找到 `<Directory>` 标签,将 `AllowOverride` 属性设置为 `All`: ``` <Directory "/path/to/laravel/public"> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ``` 3. 创建 `.htaccess` 文件,将以下代码复制进去: ``` <IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews </IfModule> RewriteEngine On # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301] # Handle Front Controller... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule> ``` 4. 保存 `.htaccess` 文件并上传到 Laravel 项目的 `public` 目录下。 以上就是在 Apache 中配置 Laravel 伪静态的步骤。 如果你使用的是 Nginx,可以按照以下步骤配置: 1. 打开 Nginx 的配置文件 `nginx.conf`,找到 `server` 标签,添加以下代码: ``` location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { fastcgi_pass unix:/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } ``` 2. 保存配置文件并重启 Nginx 服务。 以上就是在 Nginx 中配置 Laravel 伪静态的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值