my .htaccess file rewrite something wrong - php

I have a problem with .htacces file.
Can you tell me please what am I doing wrong here?
I will show you some screenshot: This image shows my Cpanel my Script files is in home/lulu/public_html/
And another problem is: When i click my friends picture his/her profile it shows me my profile but url is www.ecoshoptr.com/rajesh
last proflem is when i click my friends button page said (Not: I have no friends folder )
<li>Friends</li>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^ecoshoptr.com$
RewriteCond %{REQUEST_URI} !^/
RewriteRule (.*) http://www.ecoshoptr.com/$1
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?id=$1

Change the following two lines:
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?id=$1
to
RewriteRule ^(.*)/?$ index.php?id=$1
Your rules would only rewrite urls that has characters a-z in lower and upper case, an underscore, a - and digits 0-9. The url friends/Name wouldn't match that rule because it has a / in it. The rule I have will accept that. Also I combined the two rules into the by adding a ? after the / because it will say that it might be there and it might not be there.

I dont if you left it out but the [L] param prevents it from further rewriting and thus not ending up in your last rules
Maybe you can turn on your RewriteLog if that is available on your apache version
RewriteRule foo bar [L]

Related

Changing URLs when using $_get to determine webpage

I currently use $_GET['base'] to determine which homepage that the user visits.
This results in localhost/?base=administrator or localhost/?base=guest
I am also using this to control which page is the user at, such as
localhost/?base=guest&page=register
Is there any way to use mod_rewrite, or htaccess, to change how this system works?
Modifying my code is not an issue, is this possible?
EDIT:
I am trying to achive this:
localhost/?base=guest to localhost/guest
localhost/?base=admin to localhost/admin
localhost/?base=guest&page=register to localhost/guest/register
Below is my htaccess file
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
Will the document path affect how it is being called? As I am using a case loop to include which items are needed.
This, however, works for localhost, but it will loop every other address to main.
RewriteEngine On
RewriteRule ^$ /index.php?base=guest[L]
But did not give a result as expected.
Your rules in .htaccess need to be in reverse order, like below:
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
That is because if it is kept in the order you have it, both localhost/?base=guest&page=register & localhost/?base=administrator will match the rule RewriteRule ^([^/]*)$ /?base=$1.
Having them in reverse order ensures that the first rule is matched only for localhost/?base=guest&page=register. It won't match the first rule for localhost/?base=administrator. I hope that helps.
You need to exclude your existent files and folders from the rule
RewriteEngine On
# if the request is a dir
RewriteCond %{REQUEST_FILENAME} -d [OR]
# or file
RewriteCond %{REQUEST_FILENAME} -f
#do nothing
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
So you can use this simple code:
RewriteEngine on
RewriteRule ^(\w+)$ index.php?base=$1 [L]
RewriteRule ^(\w+)/(\w+)$ index.php?base=$1&page=$2 [L]
\w will match symbols a-z, 0-9 and underscore _, I think those characters are enough for your case, but if you need expansion it will be easy
Also in this case you don't need to change your code, because you still get base and page parameters in the $_GET array
UPDATE:
to disable query string params page and base (other params may be needed) add these two lines to the code at the bottom:
RewriteCond %{THE_REQUEST} (\?|&)(page|base) [NC]
RewriteRule .* - [L,R=404]

Cleanning urls with htaccess

i am trying to use RewriteRules to get clean urls using my HTACCESS file.
here is what i have so far
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
The above code takes a url that looks like this company.com/about.php and turns it into company.com/about/ so all my links url are like this "/about/" i didnt add the .php because of the rewrite rule.
what i am trying to do now is add a rule that will clean my url when parameter is passed. for example
company.com/about/?profile=member_name i want it to look like company.com/about/member_name
i have tried the two rewrite code below but it doesn't work.
RewriteRule ^([a-zA-Z0-9]+)$ /our_work.php/ [L]
RewriteRule ^/our_work.php([^/\.]+)/?$ ?project=$1 [L]
please keep in mind that my file extension is already being striped from the url.
Please help
Thank you in advance
Apache provides documentation on the use of RewriteRule that you should read before proceeding.
In this particular case, you can rewrite /about/{member_name} to /about.php?profile={member_name} by implementing this rule:
RewriteRule ^about/([a-zA-Z0-9]+)(/)?$ about.php?profile=$1 [L]
The rule states: The requested URL must begin with about/ and be followed by letters and numbers (match #1) and may be proceeded by an additional ending forward slash (match #2); and it will be substitued with a URL about.php with profile query string's value as match #1. The L flag indicates that any rules that follow this rule should be ignored.
Attempting to "cascade" rules will not work with your current set of rules because the /about/{...} is not matched by the first rule.

.htaccess url rewrite rules problems

I need some help to write a rewrite rule : I tested many many things but i guess i'm doing something wrong.
I need to rewrite this kind of url:
this is the FROM url :
http://website.com/a-section-a/a-section-b/a-section-c/99999-name-name2#
to:
this is the TO url :
http://website.com/index.php/newsection/99999-name-name2
I tried many thing but actually i get it:
RewriteRule /index.php/newsection/ \/([a-z]+([-]|[\/]))+
But not working ( rewrite engine ON ).
edit : The url should redirect to the TO page AND rewrite it.
A bit unclear on how your rewrite rule should work due to your syntax and the odd placement of /index.php/, but try using this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/(.*)?$ /index.php/newsection/$4 [R,L,NC]
Just so you understand how it works, the RewriteEngine On simply tells Apache to turn on the rewrite rule engine in the ruleset. The RewriteCond %{REQUEST_FILENAME} !-f assures the rule only kicks in of there isn’t a file with the same name. Similarly, the RewriteCond %{REQUEST_FILENAME} !-d assures that the rule does not kick in if there isn’t a directory with the same name.
Now the actual RewriteRule breaks down like this:
Each ([a-z0-9-]+) represents a segment of the URL path. It only matches the letters a-z (case insensitive) & numbers 0-9 as well as the - character.
The / designates each path part like a real URL.
The last part of the path is (.*)?$ which will catch anything`.
The area past the regex stuff that matches the URL is the redirect destination with $4 matching the last thing captured by the regex stuff.
And the [R,L,NC] are Apache rewrite rule flags that equate to: R means redirect, L means last meaning the ruleset stops processing & NC means match the rule with “no case” (aka: case-insensitive).
try below code,
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.html HTTP/
RewriteRule ^index.html$ http://www.yoursite.com/ [R=301,L]

rewrite URL using .htaccess only works on first query string

I'm trying to rewrite my url using htaccess but it only worked on the first query string
what I want is to rewrite this URL
acnologia.com/index.php?page=images&tag=nature
into something like this acnologia.com/images/nature
it does work on the first query string acnologia.com/images
but doesn't work if i add another directory after "images"
this is my .htaccess code:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1&tag=$2
There's only one group in your original rule, so there's no $2.
Try this one:
RewriteRule ^([a-z\d_-]+)/([a-z\d_-]+)$ index.php?page=$1&tag=$2 [NC,L]
\d equals 0-9, and NC means nocase
The very basic rule will be like this
RewriteRule ^(.*)/(.*)$ index.php?page=$1&tag=$2
And more specific will be
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2
which #Andrew answered in comment.
Your final rewrite rule should be following. RewriteCond is specified so that js, css, png etc. files do not match this rewrite rule.
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2 [L,NC]
This tutorial is helpful.

.htaccess mod_rewrite check if querystring var is avail

Basically I want to rewrite my urls so that it is website.com/folder/ sometimes though I need it to rewrite also website.com/folder/page/
Currently I have it working with just the website.com/folder/ but can not get it to check if there is a page, if I create just another rule under the folder one it reads that one, and gives me an empty page var, which is breaking my php. I struggle with .htaccess and any help would be appreciated.
Here is what I have that works with just the folder but I can not include a page.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|docs)/
RewriteRule ^([^/]*)/$ /?folder=$1 [QSA]
Here is what I tried to get it to work with either just a folder, or a folder and page
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/$ /?folder=$1 [QSA]
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/([^/]*)/$ /?folder=$1&page=$2 [L,QSA]
Please Help!
Accordingly to the RewriteRule docs you should reverse the rules order in your rules set. Because in your configuration both rules have the same RewriteCond, the most specific rule (folder + page) should be atop and the most general rule should be the last one. If not when the first rule is matched the URL is rewritten and the second rule never matches. Also, probably you want to remove the trailing forward slash in the pattern of your folder + page rule (assuming that the second group in the pattern matches a page not a folder). So I think the whole thing should read:
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/([^/]*)$ /?folder=$1&page=$2 [L,QSA]
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/$ /?folder=$1 [L, QSA]

Categories