.htaccess redirect subfolders - php

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 :)

Related

Having some issues with htaccess mod rewrite

Hello everyone wondering if i can pick your brains and il be as descriptive as possible.
So i am trying to create a page that will load a title and category (dynamic from database) based upon the selection that is picked by the user
sitename used as my domain name
So to be a little clearer i have a file and variables named sitename/2017/slot.php?gp=category&sn=title
the category and title are stored in the database for each row what i want to do is make a clean URL to look like this sitename/2017/slot/category/title/
So when viewing the page like this sitename/2017/slot.php?gp=category&sn=title the page loads everything in and displays correctly and the variables are being set as i can see them in my title in the browser tab
my error comes when i use the mod rewrite to make it clean, once the .htaccess has been changed and uploaded and i navigate to the new clean URL sitename/2017/slot/category/title/ it shows me the variables havent been set as they no longer appear in the title tab of the browser and also the page doesnt use my css. Below i have added my htaccess rule, any help and feedback is greatfully appreciated
#change this sitename/2017/slot.php?gp=category&sn=title
#to this sitename/2017/slot/category/title/
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^slot/([a-z-]+)/([a-z-]+)/ slot.php?gp=$1&sn=$2 [NC,L]
just incase here is the php code im using in slot.php i am connecting to my database fine no issues there and after the code below im running a while loop to get out the data for the row the user clicked on
if($_GET['gp'] && $_GET['sn']){
$gp = preg_replace('#[^a-zA-Z-]#i', '', $_GET['gp']);
$sn = preg_replace('#[^a-zA-Z-]#i', '', $_GET['sn']);
}
here is the full code i have in my htaccess
#make custom page not found
#ErrorDocument 404 /filenotfound.php
#stop directory file listing in all folders
IndexIgnore *
#make pages have www. in them
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.org
RewriteRule ^(.*)$ http://www.example.org/$1 [R=permanent,L]
#change this slot.php?gp=category&sn=title
#to this sitename/2017/slot/category/title/
Options -MultiViews +FollowSymLinks
RewriteRule ^slot/([a-z-]+)/([a-z-]+)/ /2017/slot.php?gp=$1&sn=$2 [R=302]
If your URL starts with 2017 you must include it in your RewriteRule.
RewriteRule ^2017/slot/([a-z-]+)/([a-z-]+)/ slot.php?gp=$1&sn=$2 [NC,L]
You're misusing the ^, which matches the very start of the path (the part after sitename/. That is, a rule starting with ^slot/ will match only sitename/slot/, not sitename/2017/slot/. Your rule should look like this:
#change this sitename/2017/slot.php?gp=category&sn=title
#to this sitename/2017/slot/category/title/
RewriteRule ^2017/slot/([a-z-]+)/([a-z-]+)/ slot.php?gp=$1&sn=$2 [NC,L]
May I join the party?
RewriteRule ^sitename/2017/slot/([a-z-]+)/([a-z-]+)/ /slot.php?gp=$1&sn=$2 [NC,L]
If the sitename the domain name, I will change my answer to :
RewriteRule ^2017/slot/([a-z-]+)/([a-z-]+)/ /slot.php?gp=$1&sn=$2 [NC,L]
Which is almost the same as Dekel's, but not exactly :)
#example.com/2017/slot.php?gp=category&sn=title
#example.com/2017/slot/category/title/
You'll need to disable MutliViews. Near the top of your .htaccess file:
Options -MultiViews
With MultiViews enabled (part of mod_negotiation) /2017/slot is being implicitly rewritten to /2017/slot.php (without any query string paramaters) before your mod_rewrite directives are being processed.
Although it's still not clear where your .htaccess file is located. So, there may still be some work to do... If your .htaccess file is located at example.com/2017/.htaccess then your existing mod_rewrite directives should be sufficient.
However, if your .htaccess file is in the document root (ie. example.com/.htaccess) then you will need to modify your RewriteRule:
RewriteRule ^2017/slot/([a-z-]+)/([a-z-]+)/ 2017/slot.php?gp=$1&sn=$2 [NC,L]

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.

.htaccess writerule to remove question marks and equal mark

today i tried to create .htaccess file that replacing ? and = with / ,
test.php code:
<?php
echo $_GET['myparam'];
?>
.htaccess:
i used this writerule:
RewriteEngine On
RewriteBase /
RewriteRule ^test/myparam/([0-9]+)/?$ test.php?myparam=$1 [NC,QSA,L]
ok, i navigated to www.web.com/test.php?myparam=123
there is no redirect to www.web.com/test/myparam/123
so navigated to: www.web.com/test/myparam/123 (Manually) and the php script is worked,
i changed the myparam value to abc instead of 123 : www.web.com/test/myparam/abc
and then it redirects to 404 not found page...(the server don't know that abc is not directory when integer works when string 404)
!so what i want to do:!
www.web.com/ test .php? inttParam = 1 & strrParam = stringhere & p = 1
TO
www.web.com/ test/inttParam/1/strrParam/stringhere/p/1
and when i use $_GET['p'] it will work.
i changed the myparam value to abc instead of 123 and it didn't work
Well of course it won't work since your rule is matching only numbers in the end:
RewriteRule ^test/myparam/([0-9]+)/?$ test.php?myparam=$1 [NC,QSA,L]
change your rule to this to make it work with anything:
RewriteRule ^test/myparam/([^/]+)/?$ test.php?myparam=$1 [NC,QSA,L]
To make it recursion based generic rule to convert /test/inttParam/1/strrParam/stringhere/p/2 to /test.php?p=2&strrParam=stringhere&inttParam=1`:
RewriteRule "^(test)(?:\.php)?/([^/]+)/([^/]*)(/.*)?$" /$1.php$4?$2=$3 [NC,L,QSA]
RewriteEngine on
# do not affect files
RewriteCond %{REQUEST_URI} !(\..{2,4})$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ index.php?parameter1=$1&%1 [L]
Something like that perhaps?
ok, i navigated to www.web.com/test.php?myparam=123
there is no redirect to www.web.com/test/myparam/123
I don't know if that means that you also want to make a redirection (presumably 301) from the one with GET params to the one with slashes.
If so, I would recommend to use the previous answer from anubhava and handle any 301 redirection with PHP. Actually, you can redirect with "RewriteRule" using the "R" flag, but I admit that I woundn't know how to solve this particular case.
Anyway, I think there is no need, unless old URLs with GET params were working well at SEO and you didn't wan't to lose ranking.

htaccess Rewrite Rule Exception

I have the following code:
RewriteEngine On
RewriteRule ^user/([0-9]+)/([0-9]+)/?$ template.php?user_id=$1&slide=$2 [NC,L,QSA]
So when I go to www.mydomain.com/user/7/1 it is working, and pulling from www.mydomain.com/template.php?user_id=7&slide=1
Now my question is, how do I get the slide variable to be optional. I want it to work if someone will type in /user/7/.
In my template.php file I have:
if(!$current_slide) {
header("Location: user/".$user_id."/1");
}
So if I manually go to template.php?user_id=1 it will automatically redirect to /user/7/1/ which is good, it works. But I need to modify my htaccess file so if someone types in /user/7/ it will redirect to /7/1/
Hope that makes sense.
RewriteRule ^user/([0-9]+)/?([0-9]*)/?$ template.php?user_id=$1&slide=$2 [NC,L,QSA]

remove http from URL in .htaccess (mod_rewrite)

Been stuck on this for ages and tried loads of fixes but just can't get my head around it!
I run a site where the content of the pages are generated based upon a URL. For example:
http://www.mysite.com/http://www.supercheese.com
Would generate a mashup of content from mysite.com and supercheese.com
The .htaccess i use for this (at mysite.com) is:
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteRule assets/.* - [L]
RewriteRule ^(.*)$ test.php?url=$1 [NC,L]
</ifModule>
So basically the second URL is passed in a php string.
My question is this, I need to remove the http:// from the address using .htaccess
E.G. If someone types:
http://www.mysite.com/http://www.supercheese.com
I need it to become:
http://www.mysite.com/www.supercheese.com
Many thanks in advance for taking a look at this. You guys rock.
You can simply use a RedirectMatch
http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirectmatch
Try with :
RedirectMatch ^/https?://(.*)$ http://www.mysite.com/$1
Edit : you have to put this before rewrite rules
Edit : add / before http
Edit : David is right, take a look a his answer and change your way of writing these urls
It looks to me like the url scheme is inherently problematic.
Using an unencoded colon : in the url - other than following the http to specify the access protocol - seems to make the server think that it is doing authentication of the form http://username:password#hostname/.
I know it doesn't directly answer the question, the solution is to change the url-scheme. ;-(

Categories