I have the following list of URLs to rewrite:
1 write url
/products/client/
send to
/basedir/system/index.php?client=cliente
2 write url
/product/client/index.php
send to
/basedir/system/index.php?client=cliente
3 write url
/products/client/image/dir2/myimage.jpg
send to
/basedir/system/image/client/dir2/myimage.jpg
4 write url
/products/client/image/dir2/more_x_dir/other.img
send to
/basedir/system/image/client/dir2/more_x_dir/other.img
With these rules I have more or less solved the points 1 and 2:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/([a-zA-Z]+)$ /basedir/system/index.php?client=base=$1 [L,QSA]
RewriteRule ^product/([a-zA-Z]+)/$ /basedir/system/index.php?client=base=$1 [L,QSA]
RewriteRule ^product/([a-zA-Z]+)/(.*)$ /basedir/system/index.php?client=$1 [L,QSA]
My problem is in cases 3 and 4 when I have files with css / image's / js. and also when I have many directories, it may be that in the case of many directories have to make a rule for everyone, but I do not know how.
thank you very much
Edit
my solution based on the answer accepted:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/([a-zA-Z]+)/(.*)\.(gif|jpg|ico|css|js|txt|zip|xls|doc)$ /basedir/system/$2.$3 [L,QSA]
RewriteRule ^product/([a-zA-Z]+)$ /basedir/system/index.php?database=$1 [L,QSA]
RewriteRule ^product/([a-zA-Z]+)/$ /basedir/system/index.php?database=$1 [L,QSA]
RewriteRule ^product/([a-zA-Z]+)/index.php$ /basedir/system/index.php?database=$1 [L,QSA]
RewriteRule ^product/([a-zA-Z]+)/(.*)$ /basedir/system/$2?database=$1 [L,QSA]
I believe that you want to do something like the following, based on your description (I'm still a little confused about the rules that you currently have). I also make the assumption that you had folders named image, css, and js for each client.
RewriteEngine On
# Rewrite images/css/js to their real files
RewriteRule ^products/([^/]+)/(image|css|js)/(.*)$ /basedir/system/$2/$1/$3 [L]
# Rewrite everything else to the index.php script
RewriteRule ^products/([^/]+)(/.+)?$ /basedir/system/index.php?client=$1 [QSA,L]
Related
So after a lot of testing - I've started to figure out my problems but still can't get the rewrite to work.
This is how my htaccess file looks now.
RewriteEngine On
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://abfielder.com/$1 [R=301,L]
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
Rule 1 is the one I'm having problems with
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
My understanding is now if I type this url into my browser
https://abfielder.com/schemdetail/id/158
I should get
https://abfielder.com/schematicdetails?id=158
The test of this here
https://htaccess.madewithlove.com/
Tells me my rules are ok.
And rules 2 and 3 are working fine.
However when I try and access
https://abfielder.com/schemdetail/id/158
I essentially get the page not found error.
Ok think I've finally fixed this for anyone else who is having issues with mod rewrite where they wish to have a rule to remove the file extension and other rewrites working together the order matters. Lastly if you're on a shared host type thing you may also need to set the rewrite base. This is how my htaccess file looks now.
RewriteEngine On
RewriteBase /
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://abfielder.com/$1 [R=301,L]
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
This allows me to have url's without the .php extension.
It also allows for
schemdetail/id/100 to be translated into schematicdetails?id=100
I got my basic redirects work with the mod_rewrite module. When requesting pages e.g. localhost/home it's correctly redirecting to localhost/index.php?page=home, but I have a problem with exceptions.
I created a folder api where I store files by category e.g. api/auth/register.php and api/customer/create.php. I tried to make rewrite rule that contains 2 params (in this example auth and customer) so basically it just drops the .php off from the url.
The rule that I made is following
RewriteRule ^api/(.*)/(.*)/?$ api/$1/$2.php [L]
After adding that line to my .htaccess, problems started to occur. For example my .css and .js files started to redirect. So maybe I need to make some exeption for the apis? Have you some other ideas to improve my rewrite rules?
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)/(.*)/?$ api/$1/$2.php [L] # problems started to occur after adding this line
RewriteRule (.*) index.php?page=$1 [L,QSA]
Thanks in advance.
RewriteCond will affect only the first following RewriteRule so you need the keep them next to your initial Rule, and move the added one above them (with its own conditions).
Also, your /api rule is not strict enough ((.*) will pick anything, including the slashes), which might not matter in you case, but still. I sugest you try with this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/([^/]*)/([^/]*)/?$ api/$1/$2.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?page=$1 [L,QSA]
I've been crazy trying to make my .htaccess work, and followed several examples with no success (of course modifying them because they were mostly static).
My problem, is that I need a generic htaccess to redirect to random php files (omiting the php extension), for example:
/xxxxx/ loads /xxxxx.php
/yyyyy/zz loads /yyyyy.php?param=zz
This is what I am trying with not success:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^MYDOMAIN [NC]
RewriteRule ^(.*)$ http://MYDOMAIN/$1 [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]
RewriteRule ^(.*?)/?(.+)$ /$1.php?param=$2 [NC,L]
</IfModule>
The problem is that I am getting 500 server error on any request.
If I comment the last rule, then:
/xxxx works correctly
/xxxx/ works correctly
/xxxx/yyyy does not work correctly (it is not rewriting the wanted
parameter).
Any help will be appreciated as I don't really know what is happening here.
P.S. I also tested to put a "stopping" rule in the beginning like:
RewriteRule ^.*\.php.*$ - [L]
And then I can have both rules without a 500 server error, just having a 404 in the parameters case.
You are getting 500 because your last rule is running without any conditions and causing infinite looping.
You can use these rules in your site root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)/([^/])/?$ $1.php?param=$2 [QSA,L]
I'm trying to allow my site to rewrite urls. I have put the following into my .htaccess file in the root directory.
RewriteEngine On
#would be nice to remove member-pages from the URL but no idea how.
#RewriteRule ^members/(.*)/?$ /$1 [NC,R]
#This part works though!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ ./members/$1/ [L]
So far, it takes
mydomain.com/someUserName or mydomain.com/someUserName/ (with trailing slash) and, if it exists, will load the page at mydomain.com/members/someUserName/ without a hitch. This works like a gem.
What I want now (and am trying to do with the first rewrite rule) is to take a mydomain.com/members/someUserName or mydomain.com/members/someUserName/ and have it show up as mydomain.com/someUserName in the url.
How do I do this? Thanks in advance!
If I understand you correctly, You want to redirect domain.com/members/foo to domain.com/foo , You can use the following rule for that:
RewriteEngine On
RewriteCond %{THE_REQUEST} /memebers/([^\s]+) [NC]
RewriteRule ^ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ./members/$1 [NC,L]
I'm working on a web application with PHP.
I'd like to have clean URLs, so I wrote a .htaccess file for it. Everything is ok, but just for 1 rule.
Look at the code below:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rule No.1
RewriteRule ^(.*)/(.*)/(.*)/?$ index.php?page=$1&id=$2 [NC,N]
# Rule No.2
RewriteRule ^(.*)/?$ index.php?page=$1 [NC,L]
This is my .htaccess file contents.
If I remove the Rule No.1, and the page be something like www.site.com/news, everything works fine. But when I want to navigate to page www.site.com/news/1/sample-text, all CSS, JavaScript, and image files will destroy (I have not access to them anymore).
Although I wrote Rule No.2 in .htaccess file, but anything did not work and the problem is steel alive.
All images, css and javascript paths are similar to ./js/jquery.js format.
Also, I put RewriteBase / in .htaccess file. But ... :(
Please help me.
Rules will be better if they are like this:
# Rule No.1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)/?$ index.php?page=$1&id=$2 [L]
# Rule No.2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?page=$1 [L]
=> You need to repeat the RewriteCond lines here: they apply only to the RewriteRule which follows.
Notes:
NC flag (case insensitivity) is useless here
the first rule doesn't use $3
([^/]+) would make more sense than (.*)