Excluding a path from a htaccess rule - php

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.

Related

Setting a rewrite rule when condition is met

fellow programmers,
It's my first time using htaccess for rewrite purposes and i can't figure out a solution for my problem.
I have created a simple rewrite rule to redirect my users to a cleaner url using php GET variables.
Here's the code
RewriteEngine on
RewriteRule ^page/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) index.php?dir=$1&admin=$2 [NC,L]
ErrorDocument 404 /page/404
For now this example doesn't work unless both parameters are set. If i'd like to visit
mysite.com/page/dashboard
then it redirects me to 404 but if add
mysite.com/page/dashboard/random
Then i get my dashboard
I need it to work both ways, if only dir is set and if both are set
Also can i remove that /page/ directory without it messing up my styles, scripts etc? Whenever i remove /page/ and leave it just mysite.com/whatever then my styles and scripts stop working because i guess it's expecting those parameters to be met.
I know it might sound a bit confusing but hopefully someone could help me accomplish this.
So i fixed this issue, seems like the issue was because i didn't check if the url had a slash at the end or not, so i added this.
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)$ index.php?dir=$1
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ index.php?dir=$1&admin=$2
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?dir=$1&admin=$2
ErrorDocument 404 /page/404
Notice the duplicate entries.
I also added a $ sign at the end so that apache would not except any more redirects or parameters.

Use .htaccess to handle folder & its sub folders

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!

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

Multiple vanity URLs for different php files?

I am trying to build a site which has the following pages which needs vanity urls,
account.php
questions.php
profile.php
I need to display
www.mysite.com/account?req=settings
www.mysite.com/account?req=questions
www.mysite.com/account?req=answers
as following
www.mysite.com/account/settings
www.mysite.com/account/questions
www.mysite.com/account/answers
And for the other files I need the following,
etc:-
www.mysite.com/questions?id=0484684
As
www.mysite.com/questions/0484684
And
www.mysite.com/profile?id=123456
as
www.mysite.com/profile/123456
Would anyone help me because I really need to make this possible.
I would like to remove the .php extention from the url too.
I like to say that I have no experience in htaccess files.
Thanks for the help, it worked !!!
In your .htaccess file, add these rules:
RewriteEngine on
RewriteBase /
RewriteRule ^account/([^/\.]+)/?$ account.php?req=$1 [L,NC,QSA]
RewriteRule ^questions/([^/\.]+)/?$ questions.php?id=$1 [L,NC,QSA]
RewriteRule ^profile/([^/\.]+)/?$ profile.php?id=$1 [L,NC,QSA]
You should be able to add new rules based on how these work if you need them in the future.
You'll want to use a .htaccess file combined with PHP for complex manipulations like that, I wrote an article on re-writing URLs after asking a similar question on SO:
http://tomsbigbox.com/elegant-url-rewriting/

PHP Mod_rewrite

i'm new to mod_rewrite, and i'm trying to convert my web address from:
website.com/profile.php?user=andy
to the following:
website.com/user/andy
This is my following code:
RewriteEngine On
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?user=$1 [NC,L]
I researched extensively and this does seem to be the correct way to do it, but it doesn't redirect to where i want it to, it just redirects to this:
http://website.com/profile.php?user=andy
which means i must doing something wrong...
Can anyone help me out here? I would appreciate any tips.
If you want
http://website.com/profile.php?user=andy ->301-> http://website.com/user/andy
http://website.com/user/andy means http://website.com/profile.php?user=andy
They are 2 different things, you'll need 2 rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^user=([A-Za-z0-9]+)
RewriteRule ^profile.php /user/%1? [R=301,L]
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?a=b&user=$1 [L]
The first will 301 (moved permanently) redirect to the pretty url.
The second will allow your application to understand the pretty url.
Whenever you change the url scheme for a site you should take care of existing links. As such, that first rule is required/a good idea. You should not, however, need the first rule when using your own application. If your own application is generating links to profile.php?user=me - change your application code.
You have to change your URLs when outputting them in your HTML to be in the format you want (/user/andy).
mod_rewrite will rewrite /user/andy to main.php?... not the other way around.
What do you mean by my result?
mod_rewrite won't change existing links in your source code. Navigate to website.com/user/andy and you should see it work.

Categories