My site is located in https://itjmovies.com/milan/public/
and I want to rewrite the URL by .htaccess
file. From https://itjmovies.com/milan/public/
To https://itjmovies.com/milan/
but it is not working.
And also https://itjmovies.com/milan/public/auth/index.php?page=new
To https://itjmovies.com/milan/public/auth/new/
but this is also not working.
I have kept my .httaccess
file in /www/wwwroot/itjmovies.com/milan/.htaccess
My .htaccess
file:
RewriteEngine On RewriteRule ^/(.*)$ /public/$1 [R=301,NC,L] RewriteRule ^public/auth/([a-zA-Z]+) /index.php?page=$1 [QSA,L]
Thank You 🙂
Advertisement
Answer
I’m not sure that your edit made your question any clearer. In your question description the stated rewrites (from/to) appear to be the wrong way round to what I think they are intended (and conflict with the order in which you have written the directives), but anyway…
My assumptions:
/public
should not be part of the visible URL. Although your site is located in the/milan/public
directory. You are making requests of the form/milan/<anything>
.- You need to internally rewrite all requests from
/milan/<anything>
/to/milan/public/<anything>
. - Requests of the form
/milan/public/auth/<something>/
(note the trailing slash, as stated in your example) should be internally rewritten to/milan/public/auth/index.php?page=<something>
I would have 2 .htaccess
files. One in the /milan
subdirectory that simply rewrites/forwards requests to the public
subdirectory. And another .htaccess
file in /milan/public
that handles rewrites that are specific to your application.
For example:
# /milan/.htaccess RewriteEngine On # Forward all requests to the "public" subdirectory RewriteRule (.*) public/$1 [L]
# /milan/public/.htaccess RewriteEngine On # Rewrite "auth/<something>/" to "auth/index.php?page=<something>" RewriteRule ^auth/([^/]+)/$ auth/index.php?page=$1 [QSA,L]
The .htaccess
file at /milan/public/.htaccess
also serves to prevent a rewrite loop when requests are rewritten to the public
subdirectory by the .htaccess
file in the parent directory. This is because mod_rewrite directives are not inherited by default.
The QSA
flag is only required if you are expecting query strings on the original request.
The RewriteRule
pattern (1st argument) matches the URL-path relative to the directory that contains the .htaccess
file.
RewriteRule ^/(.*)$ /public/$1 [R=301,NC,L] RewriteRule ^public/auth/([a-zA-Z]+) /index.php?page=$1 [QSA,L]
A few notes on your attempt – which is close, but has a few crictical errors:
- The URL-path matched by the
RewriteRule
pattern does not start with a slash (when used in.htaccess
), so the regex^/(.*)$
will never match. - The first rule is also an external redirect (ie. exposes the
/public
subdirectory) which doesn’t seem right. Do you really want/public
in the visible URL – if so then you should be linking directly to the/public
subdirectory, not relying on a redirect? - The first rule is redirecting to
/public
in the document root, not/milan/public
. - Once corrected, the first rule will also result in a rewrite-loop (500 Internal Server Error) as it will repeatedly rewrite the request…
public/public/public/<something>
etc. - The second rule is also rewriting to
/index.php
in the document root, not/milan/public/auth/index.php
.