【问题标题】:.htaccess rewrite: Remove parameter from url.htaccess 重写:从 url 中删除参数
【发布时间】:2021-02-05 09:34:26
【问题描述】:

我想从 URL 的第一个参数创建子域,然后删除该参数。

想要的结果:

http://www.example.com/mystore          -> http://www.mystore.example.com

为此,我首先为 mystore.example.com 创建了虚拟主机

现在我正在尝试从 url 中删除这个 mystore 参数。

我在 htaccess 中尝试了以下更改。请检查。

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/mystore/

虚拟主机:

<VirtualHost *:80>
    ServerAdmin admin@gmail.com
    ServerName  mystore.example.local
    ServerAlias www.mytore.example.local

    DocumentRoot /var/www/html/Projectname/public/index.php/mystore

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

.htaccess:此文件位于“public/”文件夹中。该项目在 laravel 中。

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes +FollowSymLinks
</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]

RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^mystore(/.*)?$ http://www.mystore.example.com$1 [NC,L,R=301,NE]

</IfModule>

【问题讨论】:

  • @anubhava 我在从 DocumentRoot 中删除 index.php/mystore 后尝试过,但仍然在 url mystore 中未删除。当我运行 pragnastore.sliderstock.local/pragnastore 这个网址时,它不会删除 /pragnastore。
  • 它应该重定向到相同但它只是删除 /pragnastore 并显示像pragnastore.sliderstock.local这样的url
  • 是的,我通过虚拟主机创建了这个域。最初是“example.com/mystore”,我需要将商店名称显示为子域,例如“mystore.example.com”。

标签: regex laravel apache .htaccess subdomain


【解决方案1】:

您可以在www.example.com 的站点根目录.htaccess 中使用此规则:

RewriteEngine On

RewriteCond %{HTTP_HOST} pragnastore\.sliderstock\.local$ [NC]
RewriteRule ^pragnastore(/.*)?$ http://pragnastore.sliderstock.local$1 [NC,L,R=301,NE]

确保在RewriteEngine On行下方使用此规则。

【讨论】:

  • 我用虚拟主机代码编辑了我的问题。请检查。我在我的本地项目中尝试过这个。所以我删除了 www 并尝试了但它不起作用 RewriteCond %{HTTP_HOST} ^mystore\.com$ [NC] RewriteRule ^mystore(/.*)?$ mystore.example.com$1 [NC,L,R=301,NE]
  • 我是否可以只使用htaccess而不使用虚拟主机来设置预期的URL?
【解决方案2】:

使用this link,我通过 Laravel 路由更改解决了这个问题。因此,为此,首先我创建了虚拟主机,然后在我的路由 web.php 中进行了更改

Route::group(array('domain' => '{subdomain}.website.com'), function ($subdomain) {
    Route::get('/', 'controller@method');
});

【讨论】:

    猜你喜欢
    • 2016-10-09
    • 2014-04-08
    • 2014-05-20
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    相关资源
    最近更新 更多