Use .htaccess to handle folder & its sub folders - php

So I got this working now: /en/blog/2017/03/13/website-coming-soon leads to en/blog/blog4.php and so on.
I also want to make /en/blog point to /en/blog.php but using #RewriteRule en/blog/ en/blog.php messes up the blog posts. I think I should add a condition but can't find the correct syntax online.
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/website-coming-soon en/blog/blog4.php
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/website-for-free en/blog/blog3.php
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/that-startup-feeling en/blog/blog2.php
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/how-it-all-started en/blog/blog1.php
Help is much appreciated!

Related

.htaccess redirect subfolders

Im struggling with a .htaccess settings to get following:
To create Multi-language functionality in my CMS without duplicating the productdatabase I would like to get the content of the default language, but keeping the right url in the addressbar.
For example:
http://www.myshop.com/en/webshop get the content of http://www.myshop.com/webshop
http://www.myshop.com/en/collection get the content of http://www.myshop.com/collectie
And also for the products:
http://www.myshop.com/en/webshop/item1 get content of http://www.myshop.com/webshop/item1
http://www.myshop.com/en/collection/product1 get the content of http://www.myshop.com/collectie/product1
In short:
In case of calling the folders '/en/webshop/' and '/en/collection/' there must be a rewrite to '/webshop' and '/collectie'.
Anyone got a clue?
Thanks in advance...
What about:
RewriteEngine on
RewriteRule ^/?en/webshop/(.*)$ /webshop/$1
RewriteRule ^/?en/collection/(.*)$ /collectie/$1
The first line just enables mod_rewrite
The second line (1st rule) matches anything under /en/webshop and discards the /en
The third line is similar to the second but it also "renames" collection to collectie
If you have more folders/paths you want to redirect I 'd suggest adding the rule:
RewriteRule ^/?en/(.*)$ /$1
which is rewriting anything that starts with /en.
Hope it helps, note it is not tested
Use below rule:-
RewriteEngine on
RewriteRule ^en/(.*)$ /$1 [L,R=301,QSA]
OR
RewriteEngine On
RewriteRule ^(.*)/en/(.*)$ $1/$2 [R=301,L]
Hope it will help you :)

Excluding a path from a htaccess rule

Our .htaccesss file has the following rules which is affecting access to our admin pages.
The rules are setup to allow something like the following:
http://www.example.com/en-gb/section/product/
#Most pages are caught by this so we can use the translated token stored in cms rather than having to fix here
RewriteRule ^([a-zA-Z]{2})-([a-zA-Z]{2})/([a-zA-Z0-9\-_]+)(/?)$ index.php?html/pages/$3 [L,NC]
#Product pages
RewriteRule ^([a-zA-Z]{2})-([a-zA-Z]{2})/([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)(/?)$ index.php?html/pages/$4 [L,NC]
#Product subscription pages
RewriteRule ^([a-zA-Z]{2})-([a-zA-Z]{2})/([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)(/?)$ index.php?html/pages/$5 [L,NC]
Unfortunately this is affecting the admin pages, e.g. http://www.example.com/en-gb/admin which is firing a 404.
Basically, is there any way to ignore the above rule if the path contains /admin?
I've tried a RewriteCond before the RewriteRule but it doesn't seem to be working.
Cheers!
Thanks #JustinIurman but I couldn't get your answer to work unfortunately.
It was a much simpler solution than I thought in the end.
I just changed the rule to the following:
+RewriteRule ^([a-zA-Z]{2})-([a-zA-Z]{2})/(?!admin)([a-zA-Z0-9\-_]+)(/?)$
Thanks for your help.

Rewrite Rule for htcaccess

I think this has been discussed over here in past but i don't know exactly what i have to search for so please if you could help just give me a hand.
Well i want to create a url rewrite with multiple options. I cannot explain here with few so i would rather give a simple example.
A url:
example.com/info.php?id=1
example.com/info.php?id=1&edit
I want to rewrite to like this
example.com/info/1
example.com/info/1/edit
I think you want the opposite: example.com/info/1 -> example.com/info.php?id=1
RewriteEngine on
RewriteRule ^info/([^/]+)/edit/?$ info.php?id=$1&edit [L]
RewriteRule ^info/([^/]+)/?$ info.php?id=$1 [L]

mod_rewrite, how to rewrite links correctly

I am trying to rewrite links on my site.
Possible links before rewrite and what I want to see after rewrite:
/index.pl?mode=users&action=add - /users/add/
/index.pl?mode=streets&action=edit&id=7 - /streets/edit/7
/index.pl?mode=users&action=info&id=7 - /users/info/7
etc
.htaccess content:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteRule ^([A-Za-z0-9-]+)/((edit|delete|info))/([0-9]+)$ index.pl?mode=$1&action=$2&id=$3 [L]
RewriteRule ^([A-Za-z0-9-]+)/((add))/?$ index.pl?mode=$1&action=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.pl?mode=$1 [L]
/users/add/ or /street/add/ are working properly, but...
Problems:
/users/edit/xx - I can't accept ID in perl script. Why?
/users/info/xx - I can't even get to info section /?mode=users&action=info&id=7 page (it have to show blank table with wrong ID
Btw... My site has 'switch' structure. I mean if mode is users, then it load "users.pl", if mode=streets loading "streets.pl", etc. About 2nd problem - sure I have info section at users.pl! And link /?mode=users&action=info&id=7 work perfect.
p.s.: added php-tag because it is not 'perl' problem , but php is equal to perl, so php-followers can help me too
p.p.s.: sry for my not very good english.
You have too many parentheses, which makes your backreferences off-by-one:
RewriteRule ^([A-Za-z0-9-]+)/(edit|delete|info)/([0-9]+)$ index.pl?mode=$1&action=$2&id=$3 [L]
RewriteRule ^([A-Za-z0-9-]+)/(add)/?$ index.pl?mode=$1&action=$2 [L]
Those two rules had too many parentheses around the ((edit|delete|info)) and ((add)) (though the "add" isn't affected because $2 correctly backreferences it).

.htaccess rewriterule and fake directories

I am trying to do some URL rewriting with Apache and PHP. This is for SEO reasons so that foo.com/product-category/product-title becomes foo.com/product.php?id=999. I would of course want the URL not to change and be the SEO friendly version.
I have a current site which has a working setup but it's very much a hack rather than a maintainable solution, so I am trying to do things right!
At the moment I have:-
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/?($) /url_redirect.php?page=$1 [QSA,L]
Which works perfectly, http://foo.com/random-url?foo=1&bar=2 ends up at /url_redirect.php with $_GET['page'] = 'random-url' and $_GET['foo'] = 1 etc etc..
I want the exactly the same thing to happen if you one level deeper
(eg http://foo.com/non-existing-directory/random-url?foo=1&bar=2)
I could make the non-existing-directoy a real directory and then .htaccess from there but there has to be a easer way.. I would prefer me not to have to hardcode any directory names (which is sort of how my current solution works).
I think the issue is probably something not right with the regular expression, they always defeat me.
I just tried this, might not be the best way to achieve it though (I'm no rewrite guru)
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/([a-zA-Z0-9_\ \-\%]+)?($) url_redirect.php?page=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/?($) url_redirect.php?page=$1 [QSA,L]
Sorry that was for my local setup here's how I think you would want it!
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/([a-zA-Z0-9_\ \-\%]+)?($) /url_redirect.php?page=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/?($) /url_redirect.php?page=$1 [QSA,L]
Notice the / before url_redirect.php

Categories