Htaccess rewrite not liking dashes - php

I use the following rewrite rule to behind the scenes rewrite addresses such as /pageName and /pageName/ to index.php?page=pageName. It works perfectly except when the pageName contains a -.
RewriteEngine On
RewriteRule ^(\w+)$ /index.php?page=$1
RewriteRule ^(\w+)/$ /index.php?page=$1
How can I get these rewrite rules to accept any input (or at least input containing A-Z, 1-9, and -'s.
Thank you.

You (probably...) do not want to rewrite any existing files, so you could use something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w\-]+)/?$ /index.php?page=$1
Note that I made the trailing slash optional.

RewriteEngine On
RewriteRule ^([0-9a-zA-Z-]+)$ /index.php?page=$1
RewriteRule ^([0-9a-zA-Z-]+)/$ /index.php?page=$1
This should work.

\w is the equivalent of [0-9a-zA-Z_]. e.g. upper+lower case alphabetical characters, and the undercore. - is NOT part of it, so you'll have to explicitly allow it:
^([\w\-])$
by creating your own character class.

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 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]

Categories