I'm new to htaccess usage...
need help on the below
Form http://mysite.com/index.php?page=login to http://mysite.com/login
i used the following rule in htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1
in the same, i have to restrict access to mysite.com/admin folder
meanwhile if i enter
`http://mysite.com/admin` then it goes to `http://mysite.com/admin?page=admin`
i think this is bcoz of the above rule.
What i need is, if i enter mysite.com/admin that should go for mysite.com/admin/index.php
Add a condition for rewrite rule:
RewriteEngine on
RewriteCond $1 !^admin
RewriteRule ^(.*)$ index.php?page=$1 [L,NC]
Related
I have the following .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^/(.*)/(.*)/(.*)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L,R=301]
When I access my website using http://www.domain.com/testpage/ it gives me the 404 Not Found error. What am I doing wrong?
PS: currently the index.php files just echo the pageLevel1, pageLevel2 and pageLevel3 values.
Remove the leading slash from Rewrite pattern
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/(.*)/(.*)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L,R=301]
First of, I don't know why are you using the 301 redirection at all? If you want to accept all characters in the url you just need to have a catch-all rewrite rule like this:
RewriteRule ^(.*)$ index.php?param=$1 [L]
If you want to catch the parameters like you defined, the rewrite rules can be like this:
RewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)/([A-Za-z-]+)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L]
UPDATE:
The completed rules in your case (three page levels), with URL slugs which accepts letters and numbers can be like:
RewriteRule ^([A-Za-z0-9-]+)$ index.php?pageLevel1=$1&pageLevel2=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?pageLevel1=$1&pageLevel2=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?pageLevel1=$1&pageLevel2=$2&pageLevel3=$3 [L]
This way you can access pages like:
www.domain.com/test
www.domain.com/test/another-test
www.domain.com/test/another-test/new-level
I have following rewriterules:
RewriteEngine On
RewriteRule ^([^\-]*)-(.*)-von-(.*)\.html$ $1index.php?filter=$2&marke=$3 [QSA]
RewriteRule ^([^\-]*)-von-(.*)\.html$ $1index.php?marke=$2 [QSA]
RewriteRule ^([^\-]*)-(.*)\.html$ $1index.php?filter=$2 [QSA]
RewriteRule ^(.*)\.html$ $1index.php
The first 3 rules are working, but the fourth, just rewrite index.php to .html is not. What is wrong here?
EDIT:
The URL is example.com/folder/subfolder/index.php
In the folder I got following htaccess:
RewriteEngine on
RewriteRule ^subfolder(.*) subfolder/$1
And the htaccess in subfolder is the one above
Now the URL for the first rule is example.com/folder/subfolder-value1-von-value2.html and works, for the second and third rule it's example.com/folder/subfolder-value1.html and example.com/folder/subfolder-von-value2.html
So with logic the fourth rule should also work just without the parameters but it's not working
Place this in /folder/.htaccess:
RewriteEngine on
RewriteRule ^subfolder\.html$ subfolder/index.php [L,NC]
RewriteRule ^subfolder(.+) subfolder/$1 [L,NC]
You shouldn't add $1 to the second part of the rule.it'll append the first matching group to the index.php file name. what you are currently getting is 'indexindex.php'
if you just want to rewrite index.html to index.php then you can place following line at the end of the file.
RewriteRule ^index.html$ index.php [L,NC]
also you might wanna remove $1 part from other lines as well.
Can you try this & see ?
RewriteEngine on
RewriteRule ^(.*)\.html $1\.php
I'm trying to rewrite my url using htaccess but it only worked on the first query string
what I want is to rewrite this URL
acnologia.com/index.php?page=images&tag=nature
into something like this acnologia.com/images/nature
it does work on the first query string acnologia.com/images
but doesn't work if i add another directory after "images"
this is my .htaccess code:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1&tag=$2
There's only one group in your original rule, so there's no $2.
Try this one:
RewriteRule ^([a-z\d_-]+)/([a-z\d_-]+)$ index.php?page=$1&tag=$2 [NC,L]
\d equals 0-9, and NC means nocase
The very basic rule will be like this
RewriteRule ^(.*)/(.*)$ index.php?page=$1&tag=$2
And more specific will be
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2
which #Andrew answered in comment.
Your final rewrite rule should be following. RewriteCond is specified so that js, css, png etc. files do not match this rewrite rule.
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2 [L,NC]
This tutorial is helpful.
Basically I want to rewrite my urls so that it is website.com/folder/ sometimes though I need it to rewrite also website.com/folder/page/
Currently I have it working with just the website.com/folder/ but can not get it to check if there is a page, if I create just another rule under the folder one it reads that one, and gives me an empty page var, which is breaking my php. I struggle with .htaccess and any help would be appreciated.
Here is what I have that works with just the folder but I can not include a page.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|docs)/
RewriteRule ^([^/]*)/$ /?folder=$1 [QSA]
Here is what I tried to get it to work with either just a folder, or a folder and page
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/$ /?folder=$1 [QSA]
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/([^/]*)/$ /?folder=$1&page=$2 [L,QSA]
Please Help!
Accordingly to the RewriteRule docs you should reverse the rules order in your rules set. Because in your configuration both rules have the same RewriteCond, the most specific rule (folder + page) should be atop and the most general rule should be the last one. If not when the first rule is matched the URL is rewritten and the second rule never matches. Also, probably you want to remove the trailing forward slash in the pattern of your folder + page rule (assuming that the second group in the pattern matches a page not a folder). So I think the whole thing should read:
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/([^/]*)$ /?folder=$1&page=$2 [L,QSA]
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/$ /?folder=$1 [L, QSA]
I haven't worked with .htaccess too much. Looking to do something like this:
When user types in domain.com/path it is then pulled from domain.com/index.php/path/to/file
UNLESS
someones goes to domain.com/admin
How can an add a exception or if statement for a few words that i dont want to redirect?
Put a rule for the admin rewrite first and use [L] at the end to tell .htaccess this is the last rule it should follow. Then, put your other rules after it.
Example:
RewriteRule ^admin(/?)$ - [L]
RewriteRule ^(.*)$ /path/to/file.php?id=$1
Using the - after the match rule means it won't rewrite it, it'll just go to the path that was provided.
You need to add a RewriteCond rule to exclude stuff from the rewrite rule.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^admin
RewriteCond %{REQUEST_URI} !^index.php
RewriteRule ^(.*)$ /index.php/$1 [L]