Hapus Subfolder [Codeigniter] hanya untuk URL tertentu menggunakan Htaccess

Sep 18 2019

Struktur Folder Saya

  • Domain Root
    • assets / (berisi css, gambar, folder js)
    • subfolder / (berisi Project Codeigniter di dalam dan .htaccess terpisah)
    • index.php
    • contact.php
    • .htaccess

Root Htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)subfolder
RewriteRule ^(.*)$ subfolder/$1 [L]

Subfolder (Proyek Codeigniter) Htaccess

DirectoryIndex index.php index.html

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond $1 !^(index\.php|assets|install|update) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # For godady Shared Hosting Server uncomment the line below RewriteRule ^(.*)$ index.php?/$1 [L]


</IfModule>

Daftar URL Subfolder

  • http://example.com/subfolder/products/
  • http://example.com/subfolder/product/product-slug-123

Daftar URL Root

  • http://example.com/index.php
  • http://example.com/contact.php

Dalam Proyek Codeigniter saya, saya telah menetapkan Productscontroller sebagai default_controller. Jadi masalahnya, ketika saya mencoba mengakses halaman index.php di folder Root, itu hanya menampilkan daftar produk Codeigniter sebagai halaman Beranda.

Saya perlu URL berikut:

  • http://example.com/index.php
  • http://example.com/contact.php
  • http://example.com/products/
  • http://example.com/product/product-slug-123

Ada solusi dengan htaccess atau dengan kode?

Jawaban

1 SilambarasanR.D Sep 18 2019 at 14:20

Akhirnya saya mendapatkan ini bekerja.

RewriteCond %{REQUEST_URI} !((.*)subfolder|^/assets) // to Exclude this
RewriteCond %{REQUEST_URI} (^/products(.*)|^/product(.*)) // to Include this
RewriteRule ^(.*)$ subfolder/$1 [L]

Simbol tanda seru (!) Digunakan untuk 'tidak' mencocokkan url.

Jadi saya menghapus simbol Exclamation dan menggunakan RewriteCond (Kondisi) lain untuk mengalihkan hanya jika url berisi segmen sebagai productsatauproduct http://example.com/products/ atau http://example.com/product/product-slug-123. Jadi ini bekerja dengan sempurna. Saya pikir itu akan berguna bagi seseorang. Selamat membuat kode.