Lang parameters in url rewriting - apache

Some times now I'm around my htaccess and no way to get what I want...
I'd like to rewrite as followings :
http://www.mydomain.com/trunk/public/lang/ => http://www.mydomain.com/trunk/public/?l=lang
http://www.mydomain.com/trunk/public/lang/profile => http://www.mydomain.com/trunk/public/profile?l=lang
http://www.mydomain.com/trunk/public/lang/user/45 => http://www.mydomain.com/trunk/public/user/45?l=lang
...
Any idea how to do that?
--------EDIT-------
My htaccess actually looks like :
<IfModule mod_rewrite.c>
RewriteEngine On
#Transform lang into get parameter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/$ ?l=$1 [L,QSA]
#Transform lang into get parameter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/(.+)$ $1?l=$2 [L,QSA]
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|images|assets|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Results :
http://www.mydomain.com/trunk/public/lang/ gives a bonfire 404 error (it tries to load "lang" module)
http://www.mydomain.com/trunk/public/lang/profile gives a server 404 error
http://www.mydomain.com/trunk/public/profile works
http://www.mydomain.com/trunk/public/profile?l=fr works
I have managed to set the default domain destination in the trunk/public folder via an htaccess placed in a the trunk parent directory

Insert these 2 rules in your /trunc/public/.htaccess file:
RewriteEngine On
RewriteBase /trunc/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ ?l=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.+)$ $1?l=$2 [L,QSA]

Related

3rd rule in .htaccess to redirect to specific file

My code is following, the first 2 rules are working fine, but I'm unable to work the 3rd one(Check if its category eg. /some-folder) page should be served by category.php file. Can anyone help?
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# WORKING - Check if its a product eg. /some-folder/some-product.html
RewriteRule ^/?([^/]+)/.+\.html$ product.php [QSA,L]
# WORKING - Check if its a page eg. /some-page.html
RewriteRule ^/?[^/]+\.html$ page.php [QSA,L]
# NOT WORKING - Check if its category eg. /some-folder
RewriteRule ^/?[^/]+\$ category.php [QSA,L]
With your shown samples, please try following edited htacess rules.
Please make sure to clear your browser cache before testing your URLs.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# WORKING - Check if its a product eg. /some-folder/some-product.html
RewriteRule ^([^/]*/)/([^.]*\.html)/?$ product.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# WORKING - Check if its a page eg. /some-page.html
RewriteRule ^([^.]*)\.html/?$ page.php [QSA,NC,L]
# EDITED RULES here.. - Check if its category eg. /some-folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?[^/]+/?$ category.php [QSA,L]

Apache2 rewrite - conditional

I have the following .htaccess file in my project's web root:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /repos/eamorr.co.uk/react_www/index.html [L,QSA]
The above works.
When I navigate to:
http://localhost/repos/eamorr.co.uk/react_www
I see my homepage.
And when I navigate to:
http://localhost/repos/eamorr.co.uk/react_www/contact-us
I see the "contact-us" route of my homepage (my webpage is written in React).
OK. Great.
Now, some users are on Linux:
When they checkout the code, they see the site at a slightly different url:
http://localhost/_eamorr/develop/eamorr.co.uk/react_www/
and
http://localhost/_eamorr/develop/eamorr.co.uk/react_www/contact-us
Regarding this line of the .htaccess file:
RewriteRule . /repos/eamorr.co.uk/react_www/index.html [L,QSA]
Instead of . is there some conditional that I can put in to serve index.html from a different path?
e.g.
RewriteRule {{condition1???}} /repos/eamorr.co.uk/react_www/index.html [L,QSA]
or (different condition = different path):
RewriteRule {{condition1???}} /_eamorr/develop/eamorr.co.uk/react_www/index.html [L,QSA]
I'm afraid my Apache knowledge isn't up-to-speed to do this quickly and I've spent hours at this!
=================
Solution:
Use a cascading htaccess rewrite rule setup as follows:
RewriteEngine On
#RewriteBase /
#Linux localhost:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*repos/eamorr.co.uk/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /repos/eamorr.co.uk/react_www/index.html [L,QSA]
#Mac localhost:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*_eamorr/eamorr.co.uk/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /_eamorr/eamorr.co.uk/react_www/index.html [L,QSA]
#develop .co.uk
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*gitWebFlow/eamorr.co.uk/develop/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /gitWebFlow/eamorr.co.uk/develop/react_www/index.html [L,QSA]
#master .co.uk
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^.*gitWebFlow/eamorr.co.uk/master/react_www.*$
RewriteRule ^([^/]+\.?[^/])*$ /gitWebFlow/eamorr.co.uk/master/react_www/index.html [L,QSA]
#default
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L,QSA]
This is good enough for my needs right now.
If react_www is your project's root, and you're not referencing assets above that directory (publicly, via URLs), that's where the .htaccess should be. Then it would become
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . index.html [END]
You shouldn't need RewriteBase since you're not doing server-side external redirects, and just want virtual URLs below that directory to all serve the same file.

.htaccess Rewrite Rule doesn't work on my site

how i can rewrite this url:
http://www.example.com/album?album_id=52
to this:
http://www.example.com/album/52
my .htaccess code is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^album/$ album?album_id=$1 [L]
You need to capture album id from your pattern first before you can use it as $1:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^album/([0-9]+)/?$ album?album_id=$1 [L,QSA]

Ignore files in non-existent paths using .htaccess

I am having a problem with rewriting.
Here is my .htaccess-file
RewriteEngine On
RewriteBase /
#ignore non-existent utility-files
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt)$
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* - [L]
#load existent utility files and don't rewrite them to index.php
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt)$
RewriteRule ^(.*) $1 [L]
#load other files if they exist
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) $1 [L]
#load index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
The last part is working but I want to avoid that my index.php is loaded when the code requests a non-exisiting image (as: www.mysite.test/images/foobar.png)
The first RewriteCond-Line should check if the request matches a file of image, JavaScript, and the like and the second RewriteCond line should check if the file does NOT exist.
The third one should check if the folder exists.
If the match succeeds NOTHING should be loaded.
It works but only if the folder exists. If the folder if the image doesn't exist it doesn't work.
You have many redundant rules. You can simplify the to:
RewriteEngine On
RewriteBase /
#ignore non-existent utility-files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt)$ [NC]
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Ok there was still an issue with existing files, as Member anubhava said.
My final solution is:
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
RewriteBase /
#load files if they exist
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) $1 [L]
#ignore non-existent utility-files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt|php)$ - [L]
#load index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
This solution includes php-files in the second rewrite-rule. Because I have some php-files that are actually css-files and loaded by the browser.

htacces rewrite url - remove folder name from url

I'm tryng to make an htaccess url rewrite for this cases:
www.website.com/index.php/admin/something => www.website.com/admin/something
www.website.com/index.php/website/something => www.website.com/something
www.website.com/index.php/login/something => www.website.com/login/something
note: "something" can be "something1/something2/something3" or "something1/something2" or "something"
I can cut "index.php" with:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
but I can't found a way to remove only "website/" and not "admin/" and "login/" leaving the rest of url.
If taking this approach you will probably have to specify each possible input reference that should not be mapped to your 'website' controller and remap it to index.php/$1 and treat every other request like it is a method of the website controller, mapping it to index.php/website/$1
Something like the below will map anything starting with admin or login to index.php and everything else to the website controller:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^admin.*
RewriteCond %{REQUEST_URI} ^login.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}
RewriteRule ^(.*)$ /index.php?/website/$1 [L]