Different rewrite rule in .htaccess - php

i have already this rewrite rule in my htaccess
RewriteCond $1 !^(index\.php)
RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?id_user=$1&page=$2 [L]
http://www.mydomain.com/index.php?id_user=user&page=news
http://www.mydomain.com/user/news.html
Now i have a problem for news details and event details.
for have an url like this (only for page news):
http://www.mydomain.com/user/details-news/category/title-news.html
and this (only for page event)
http://www.mydomain.com/user/event/title-event.html
Can i insert new rule in htaccess for have these rules or i must edit my actually rules?
My url is the same for all query
index.php?id_user=user&page=details-news&category=category&title=title-news
or
index.php?id_user=user&page=event&title=title-event
Any help would be greatly appreciated!

You actual rule match only against:
^([^/]*)/([^/]*)\.html$
urls.
Which means only one / inside. So you can add other rules with more slash, without the need to modify this actual existing rule.
The only problem would be if you want to add other rules with one /. But this is not the case.

You can use these rules:
# skip further rules for files/directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/.]+)\.html$ /index.php?id_user=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/.]+)\.html$ /index.php?id_user=$1&page=$2&title=$3 [L,QSA]
RewriteRule ^([^/]+)/^([^/]+)/([^/]+)/([^/.]+)\.html$ /index.php?id_user=$1&page=$2&category=$3&title=$4 [L,QSA]

Related

Error in rewrite rule in htaccess

I was getting problem in htaccess. I have tried a lot examples but yet not able to write perfect Rewrite Rules as I want. Please help me.
http://domainname.com/sub-category.php?mc=eatry
to
http://domainname.com/eatry
AND
http://domainname.com/explorelisting.php?mc=eatry&&sc=restaurant
to
http://domainname.com/eatry/restaurant
.htaccess (I have tried)
RewriteEngine On
RewriteRule ^/(.*) ./sub-category.php?mc=$1
RewriteRule ^/(.*) ./explorelisting.php?mc=$1&&sc=$1
RewriteRule ^([^\.]+)$ $1.php [NC,L]
A few things to note.
In the URL you don't need && only & when specifying another parameter.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\w+) sub-category.php?mc=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\w+)/(\w+) explorelisting.php?mc=$1&sc=$2 [QSA,NC,L]
The regular expression assumes that the query will only accept any letter - capital or lowercase.
RewriteCond is used to check that the URL is not a directory and not a file, if it isn't, then it rewrites to the rule.
This is used because the URL http://www.example.com/index would get redirected to http://www.example.com/sub-category.php?mc=index which is undesired.
It might be prudent to restructure your rule. Perhaps having the URLs looking like http://www.example.com/category/eatry and then modifying the first rule to being RewriteRule ^category/(\w+) sub-category.php?mc=$1 in order to prevent ambiguity.
The flags (what is at the end of each rule in square brackets) are as follows:
QSA - Query String Append:
This enables you to have the parameters at the end of the string (anything following the ?).
NC - No Case:
This means that your rule is case insensitive.
L - Last:
This will terminate the htaccess if the rule is matched.

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.

Not Found when rewriting using same rule as another rewrite

So I have a rewrite doing article?id=10 which goes to article/10 and I'm trying to do another like this.
I want to go from site?=website.com to site/website.com. Alright, seems like an easy task. So I do
RewriteRule ^site/([0-9]+)$ site.php?id=$1
Which is the same rule as the article rewrite basically. Now I go to site/10 for example and I get
Not Found
The requested URL /site/sdfsd was not found on this server.
Why would this happen if it's the same thing as the other rewrite which works fine?....
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([0-9]+)$ article.php?id=$1 [L,QSA,NC]
RewriteRule ^site/([0-9]+)$ site.php?id=$1
# rewrite from /dir/file/ to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
Try
RewriteRule ^site/(.+)$ site.php?id=$1 [L]
[0-9]+ only matches numbers.
Basically your rewriting rule is bad
RewriteRule ^site/([0-9]+)$ site.php?id=$1 will only match numbers
You'll could use instead
RewriteRule ^site/(.*)$ site.php?id=$1
(wildcards)
And I'll have to say that the expression "site/whatever.com" points in reality to
site.php?id=whatever.com - reverse statement is false
You rule does not match because the role only allow numbers. In other words your rule say:
Start with "site/" following a number (0-9).
Your rule must be
RewriteRule ^site/([0-9a-zA-Z]+)$ site.php?id=$1
You seem to have two problems.
[0-9] only matches numbers, so it won't work with letters.
Also you need to add the [L] flag to your rules to make sure it stops at the first match.
RewriteRule ^site/(0-9a-zA-Z+)$ site.php?id=$1 [L]

.htaccess if statement?

I haven't worked with .htaccess too much. Looking to do something like this:
When user types in domain.com/path it is then pulled from domain.com/index.php/path/to/file
UNLESS
someones goes to domain.com/admin
How can an add a exception or if statement for a few words that i dont want to redirect?
Put a rule for the admin rewrite first and use [L] at the end to tell .htaccess this is the last rule it should follow. Then, put your other rules after it.
Example:
RewriteRule ^admin(/?)$ - [L]
RewriteRule ^(.*)$ /path/to/file.php?id=$1
Using the - after the match rule means it won't rewrite it, it'll just go to the path that was provided.
You need to add a RewriteCond rule to exclude stuff from the rewrite rule.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^admin
RewriteCond %{REQUEST_URI} !^index.php
RewriteRule ^(.*)$ /index.php/$1 [L]

Categories