I have a working RewriteRule that for example http://mydomain/stack rewrites to decoder.php?decode=stack and then redirects it to the correct website because decoder.php looks up in its database what the redirect should be.
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on
RewriteRule ^phpmyadmin$ phpmyadmin [L]
RewriteRule ^([\w\d-]{1,})$ decoder.php?decode=$1 [L]
But now I need "more levels". First I tried with a dot. Eg. http://mydomain/l1.stack, but I couldn't get my RewriteRule to work. Anyway: the better approach would be http://mydomain/l1/stack to rewrite to decoder.php?level=l1&decode=stack or http://mydomain/a1/wH4tever. to decoder.php?level=a1&decode=wH4tever.. The "levels" that I want should be fixed in the .htaccess, but the "decode" could be any string.
One problem: I don't get any RewriteRule to work with what I want :-(.
Here a simple rule for a single, fixed and literal "level" string "l1":
RewriteEngine on
RewriteRule ^l1/([\w\d-]{1,})$ decoder.php?level=$1&decode=$2 [L]
RewriteRule ^([\w\d-]{1,})$ decoder.php?decode=$1 [L]
This should do if there are multiple fixed literal "level" strings (here "l1" or "a1")
RewriteEngine on
RewriteRule ^(l1|a1)/([\w\d-]{1,})$ decoder.php?level=$1&decode=$2 [L]
RewriteRule ^([\w\d-]{1,})$ decoder.php?decode=$1 [L]
That would be the generic variant:
RewriteEngine on
RewriteRule ^([\w\d-]{1,})/([\w\d-]{1,})$ decoder.php?level=$1&decode=$2 [L]
RewriteRule ^([\w\d-]{1,})$ decoder.php?decode=$1 [L]
Test yourself: htaccess.madewithlove.com
Related
Basically I have my website set up the following way:
mysite/site -Goes to the main index.php file
mysite/site/asdf -Goes to the main index.php file with a subpage
mysite/site/admin -Goes to an admin panel with various subpages
But the issue I'm running into is when I go to mysite/site/admin without the trailing slash, it appends /?admin=1 to the URL. Same if I go to mysite/site/admin/pages without the trailing slash, it appends /?admin=1&page=pages to the URL. I want it to not append these query strings.
If I add the trailing slash, the query string does not get appended. If I go to mysite/site/admin/pages/edit without the trailing slash, it doesn't append the query string, so it seems just the first 2 levels do this.
This is confusing and I don't understand it at all. I've tried lots of various things from googling and searching this site but nothing has worked. I'm a newbie to this .htaccess stuff. Here's what my .htaccess file looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/?admin/users/([\-_A-Za-z0-9]+)/?$ admin/index.php?admin=1&page=users&subpage=view&user=$1 [L]
RewriteRule ^/?admin/users/create/?$ admin/index.php?admin=1&page=users&subpage=create [L]
RewriteRule ^/?admin/([\-_A-Za-z0-9]+)/([\-_A-Za-z0-9]+)/([\-_A-Za-z0-9]+)/?$ admin/index.php?admin=1&page=$1&subpage=$2&id=$3 [L]
RewriteRule ^/?admin/([\-_A-Za-z0-9]+)/([\-_A-Za-z0-9]+)/?$ admin/index.php?admin=1&page=$1&subpage=$2 [L]
RewriteRule ^/?admin/([\-_A-Za-z0-9]+)/?$ admin/index.php?admin=1&page=$1 [L]
RewriteRule ^/?admin/?$ admin/index.php?admin=1 [L]
RewriteRule ^/?([\-_A-Za-z0-9]+)/?$ index.php?page=$1 [L]
</IfModule>
Options -Indexes
Can anybody tell me what I'm doing wrong or how to fix this?
That is because /site/admin/pages is a valid directory and without trailing slash Apache's mod_dir module redirects the URL to one with a trailing slash.
To fix you can use:
RewriteEngine On
# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]
RewriteRule ^/?admin/users/([-\w]+)/?$ admin/index.php?admin=1&page=users&subpage=view&user=$1 [L]
RewriteRule ^/?admin/users/create/?$ admin/index.php?admin=1&page=users&subpage=create [L]
RewriteRule ^/?admin/([-\w]+)/([-\w]+)/([-\w]+)/?$ admin/index.php?admin=1&page=$1&subpage=$2&id=$3 [L]
RewriteRule ^/?admin/([-\w]+)/([-\w]+)/?$ admin/index.php?admin=1&page=$1&subpage=$2 [L]
RewriteRule ^/?admin/([-\w]+)/?$ admin/index.php?admin=1&page=$1 [L]
RewriteRule ^/?admin/?$ admin/index.php?admin=1 [L]
RewriteRule ^/?([-\w]+)/?$ index.php?page=$1 [L]
Hello I am cleaning up my urls by using the htaccess file.
I have a parameter called: page and a parameter called: id.
so my original url is:
http://bouwen040.sayhey.nl/index.php?page=leden&id=15
and I would like it to be:
http://bouwen040.sayhey.nl/leden/15
the page that calls only the 'page' parameter works. The other one is giving a page not found error.
this is my rewriterule code:
RewriteEngine On
RewriteRule ^([A-Za-z0-9\-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9\-]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9\-]+)/$ index.php?page=$1&id=$2
I also tried this one:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.php$ /index.php?page=$1&id=$2 [L]
I just don't know why one parameter works and two doesn't ?
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?page=$1&id=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L,QSA]
Ok I have the following .htaccess and it works however I can seem to change it to the way I need it.
here is the current .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|admin|system|images|tpl|js|lib|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.php?tpl=$1 [L]
Options +FollowSymlinks
I need to add the following
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&tpl=$2 [L]
So i am wondering how do I get that rule to work? as when I add it it does not.
So if you want to add a rule that will handle all requests except the first one that matches the RewriteCond And those would be split into /?
I think something like this would work. Please note that I changed the $1 to ${REQUEST_URI} in your rewrite condition
RewriteEngine on
RewriteCond ${REQUEST_URI} !^(index\.php|admin|system|images|tpl|js|lib|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.php?tpl=$1 [L]
RewriteRule ^([^/]*)/?(.*)?$ /index.php?cat=$1&tpl=$2 [L]
Options +FollowSymlinks
I have a site (sports.com, let's say) and I'd like to set up mod_rewrite so that a visitor to "football.sports.com/america" is shown content from "sports.com/stuff/place.php?id=america". I'd still like visitors to the naked "sports.com" to see "stuff/index.php".
Sorry, I'm a real newb with this. I've tried and failed several times, heres what I have so far...
IndexIgnore *
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} football\.sports\.com [NC]
RewriteRule ^([^/.]+)$ stuff/place.php?id=$1 [L]
RewriteRule (.*) stuff/index.php?%{QUERY_STRING}
Any help would be massively appreciated.
You can do this like:
RewriteRule ^america$ america/ [NC]
RewriteRule ^america/$ stuff/place.php?id=america [NC,L]
Redirects football.sports.com/america[/] to football.sports.com/stuff/place.php?id=america. This is a static rule: it won't work with france for example.
If you need a generic way you need :
RewriteRule ^([^/.]+)$ $1/ [NC]
RewriteRule ^(.*)/$ stuff/place.php?id=$1 [NC,L]
To get naked site to be redirected, add:
RewriteRule ^$ stuff/index.php [L]
Redirects football.sports.com/ to football.sports.com/stuff/index.php
Eventually you can add the query string of necessary.
I have a cakephp installation in the root of my domain. Now it turns out I need to put another app in there that will reside in a subdirectory. How do I disable the controller/model redirection in cake for just this directory?
The current .htaccess in the root folder looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
I've tried modifying it like this, but to no avail:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^bildbank$ /bildbank/ [L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
I am aware that this is a bit of a hack, but there's no way I can get the second app to play nice with cake.
Deizel's answer didn't work for me, but placing the following RewriteRule above the two cakephp rules did:
RewriteRule ^bildbank - [L]
The following rule rewrites www.example.com and www.example.com/ to www.example.com/app/webroot/
RewriteRule ^$ app/webroot/ [L]
This rule rewrites www.example.com/* to www.example.com/app/webroot/*
RewriteRule (.*) app/webroot/$1 [L]
I would throw out your rule and update the wildcard regex in the last rule, (.*), so that it matches any string which doesn't start with bildbank. Something like this might do the trick:
RewriteRule ((?!bildbank).*) app/webroot/$1 [L]
This converts the following strings:
cake
cake.htm
cake/test
bilder
bilder.htm
bilder/test
bildbank
bildbank.htm
bildbank/test
.. into:
app/webroot/cake
app/webroot/cake.htm
app/webroot/cake/test
app/webroot/bilder
app/webroot/bilder.htm
app/webroot/bilder/test
bildbank
bildbank.htm
bildbank/test
.. therefore, excluding your bildbank subdirectory.