I created the following .htaccess file and I am wondering if it could be improved:
RewriteEngine On
RewriteRule ^site$ ./site/ [L,NC]
RewriteRule ^site/(.*)$ ./main/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/en [NC]
RewriteRule ^(.*)$ ./404.php?url=$1 [L,NC]
I want to redirect visitors which enter www.domain.com/site/ to www.domain.com/main/ and all other requests to www.domain.com/404.php?url=... This works, except when the user doesn't enter a trailing slash (www.domain.com/site).
You should be able to do that with a /? like
RewriteEngine On
RewriteRule ^site$ ./site/? [L,NC]
RewriteRule ^site/(.*)$ ./main/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/en [NC]
RewriteRule ^(.*)$ ./404.php?url=$1 [L,NC]
For the first rule, you rewrite site to site/ and then rewrite site/ to main/. I would rather do it in one step, e.g.
RewriteRule ^site/?$ ./main/ [L,NC]
The second rule would stay as it is.
With the current rules, you only redirect nonexisting paths to 404.php. If this is what you want, everything is fine. If not, you should remove !-f and !-d conditions and add a condition to exclude main from being rewritten to 404
RewriteCond %{REQUEST_URI} !/main/ [NC]
Even though the substitution ./404.php is valid, I would write it as
RewriteRule ^(.*)$ 404.php?url=$1 [L,NC]
Related
In an application I want all users to start at index.php. So I want to redirect all direct URLs to other pages to this file.
When I use the following syntax, only the non-existing files are rewritten, but I want to redirect the existing URLs too.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f means: if this file does not exist, apply the RewriteRule.
RewriteCond %{REQUEST_FILENAME} !-d means: if this directory does not exist, apply the RewriteRule.
Use:
RewriteEngine on
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
EDIT:
If you want to redirect only when the URL does not start with /index.php yet, use this extra condition:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ /index.php/?path=$1 [NC,L,QSA]
I have the following .htaccessfile
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
And I have the following url:
http://localhost/mysite/loginPage/
I want to ensure that this url will never ends with forward slash (loginPage/).
I tried to use the following lines:
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^([^/]+/[0-9]+/[0-9]+/[0-9]+/[^/]+)/.+$ - [L,R=404]
But I get error message in all cases that page is not redirecting properly. Please have in mind that I want this just for the specific URL above, i don't care for the rest of the site.
It depends on where you put the rule for your rewrite. The examples you provided do work. But you have to put them before your last htaccess line.
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
This will create the proper output, and can be tested through something like a htaccess tester
If you want it specifically for just that url you could resort to:
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mysite/loginPage/$ mysite/loginPage? [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
You could also specify an absolute redirect with
RewriteRule ^mysite/loginPage/$ http://%{HTTP_HOST}/mysite/loginPage? [R=301,L]
I want this just for the specific URL above
In that case, your rule should just match specifically this URI rather than .*/.* etc.
Have your rules like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(/mysite/loginPage)/$ [NC]
RewriteRule ^ %1 [L,R=301]
RewriteCond $1 !^(index\.php|resources|robots\.txt) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php/$0 [L]
You don't really need QSA flag in last rule also.
Make sure to clear your browser cache before testing this change.
I currently have very little Apache experience, and am having difficulties with my .htaccess file. My question is this: how can I rename these files, listed below, properly? I believe my syntax is accurate, according to http://www.htaccesscheck.com, but when accessing these pages, either A: the page won't load due to a redirect loop, or B: the page won't load, but will redirect to the wrong page. Here is my current .htaccess file for this directory:
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
Any help is much appreciated.
Try code below:
RewriteEngine on
RewriteBase /photos/
RewriteRule ^([0-9]{1,2})-([0-9]{4})$ archives.php?month=$1&year=$2 [L]
RewriteRule ^([0-9]+)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
This rules will check, if:
request like yourdomain/11-1111, then return archives.php
request like yourdomain/111, then return catpost.php (you can type
any number)
else will return viewpost
You have some errors in your current .htaccess, because your second rule get result of first rule.
By the way, you can use http://htaccess.madewithlove.be/ to check step by step what is posted to your rewrite rules.
be sure to write a valid pattern
try this
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*?)-(.*?)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*?)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*?)$ viewpost.php?id=$1 [QSA,L]
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ api.php?url=$1 [L]
Above shows my current configuration.
The problem with this is it only seems to be redirecting if a directory or file exists after the folder the htaccess file is in.
How can I rewrite this so it literally just redirects every single request to api.php with the full requested path? I'm not looking to see whether directories or files exist in the htaccess file, I can do that in api.php, I just want to push all the requests through that file.
You can use:
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteRule ^((?!api\.php$).*)$ api.php?url=$1 [L,QSA,NC]
Lookahead (?!api\.php$) means rewrite everything except /api.php to /api.php with query parameter url.
like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /api.php?path=$1 [NC,L,QSA]
make sure you have this before the route
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
this is my scenario. My application urls are showed like this:
example.com/index.php/article/whatever/here
in order to remove the index.php from url I have put this in my .htaccess and works fine:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Now, my problem is that I have include and admin.php like this:
example.com/admin.php/manage/whatever/here
And I don't know how to remove the admin.php from url without make conflics with the index.php because I have tried this and a 500 Internal Server Error is showed:
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^(.*)$ /admin.php/$1 [L]
EDIT
Another try was this and nothing, because a 404 error is showed:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*?)/(.*) /$1/$2 [L]
ALL my urls should have several parameteres as requiered:
example.com/this/is/a/large/url/because/is/requiered
EDIT 2:
I have create two rules, each rule alone works fine!!! but both can't work together:
RewriteRule ^admin/(.*)$ /admin.php/$1 [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond ${REQUEST_URI} ^admin/(.*)$ [NC]
RewriteRule ^(.*)$ /admin.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Then at least test this with:
http://www.example.com/some/url
http://www.example.com/admin/some/url
You can slightly modify your rewrite rule to capture the first part of the path, and use it in the replacement. Try this:
RewriteRule ^/(.*?)/(.*) /$1/$2 [L]
Basically the () create a capturing group, so anything between the first two / will be captured as $1 and anything after that will be captured as $2
Note that you can also change your rewrite conditions, if you only want this replacement to apply to index.php and admin.php