My development URL is https://valkya.lan/trackbackemail/ And my production URL will be…
- User
My development URL is https://valkya.lan/trackback_email/ And my production URL will be https://trackback.email/
I have following folder structure for my project: /trackback_email /public .htaccess .htaccess
So, according to this rewrite content for both the .htaccess files. Because in my development environment, Rewrite all requests to the public folder is not working.
- Assistant
To ensure your Apache server correctly rewrites URLs in both your development and production environments, you need to configure two
.htaccessfiles:- Root
.htaccess(located at/trackback_email/.htaccessin development and at the root in production) - Public
.htaccess(located at/trackback_email/public/.htaccess)
Here's how to set them up for both environments:
1. Root
.htaccessPurpose: Redirect all incoming traffic to the
publicdirectory.Location:
- Development:
/trackback_email/.htaccess - Production: If your production environment serves from a subdirectory, place it accordingly. Otherwise, if
publicis the web root, this file may not be necessary.
Content:
<IfModule mod_rewrite.c> RewriteEngine On # Redirect all requests to the public folder RewriteRule ^(.*)$ public/$1 [L] </IfModule>Notes:
- This rule takes any incoming request and rewrites it to serve from the
publicdirectory. - Ensure that the
publicdirectory exists and contains its own.htaccess.
2. Public
.htaccessPurpose: Handle URL rewriting within the
publicdirectory, directing non-file/directory requests toindex.php.Location:
- Development:
/trackback_email/public/.htaccess - Production:
/public/.htaccess(assumingpublicis the document root)
Content:
For Development Environment
<IfModule mod_rewrite.c> RewriteEngine On # Set the base URL to the subdirectory path RewriteBase /trackback_email/public/ # Redirect all non-existing files or directories to index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [L] </IfModule>For Production Environment
<IfModule mod_rewrite.c> RewriteEngine On # Set the base URL to root RewriteBase / # Redirect all non-existing files or directories to index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [L] </IfModule>Explanation of Directives:
RewriteEngine On: Enables the runtime rewriting engine.RewriteBase: Defines the base URL for per-directory rewrites. It should match the URL path where your application is accessed.- Development: Since your app is accessed via
https://valkya.lan/trackback_email/, and the.htaccessis insidepublic, the base should include/trackback_email/public/. - Production: If your app is accessed directly via
https://trackback.email/, the base is simply/.
- Development: Since your app is accessed via
RewriteCond %{REQUEST_FILENAME} !-f: Checks if the requested filename does not exist as a file.RewriteCond %{REQUEST_FILENAME} !-d: Checks if the requested filename does not exist as a directory.RewriteRule ^ index.php [L]: Redirects all requests that passed the above conditions toindex.php.
Additional Considerations
-
Ensure
mod_rewriteis Enabled: These configurations rely on Apache’smod_rewritemodule. Make sure it's enabled on your server.You can enable it using:
a2enmod rewrite systemctl restart apache2 -
AllowOverride Directive: Ensure that your Apache configuration allows
.htaccessoverrides. In your Apache configuration (apache2.confor the respective virtual host file), the relevant<Directory>block should haveAllowOverride All.Example:
<Directory /var/www/html/trackback_email/public> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> -
Permissions: Verify that the
.htaccessfiles have the correct permissions and ownership so that Apache can read them. -
Testing Rewrite Rules:
- Development: Access a URL like
https://valkya.lan/trackback_email/some/pathand ensure it routes correctly. - Production: Access
https://trackback.email/some/pathto verify proper routing.
- Development: Access a URL like
-
Error Handling: If rewrites aren't working as expected, check Apache’s error logs for clues. Common issues include typos in
.htaccess, incorrectRewriteBase, or missing modules.
Summary
By setting up the
.htaccessfiles as described:-
Development Environment:
- Routes all traffic through
/trackback_email/public/. - Ensures that URLs like
https://valkya.lan/trackback_email/some/pathare correctly handled.
- Routes all traffic through
-
Production Environment:
- Assumes
publicis the web root. - Routes URLs like
https://trackback.email/some/pathtoindex.phpif they don’t correspond to existing files or directories.
- Assumes
This configuration ensures that your application seamlessly handles URL routing in both environments. Make sure to adjust the
RewriteBasedirective appropriately based on where your application is hosted. - Root