Regular expression for htaccess URL rewrite - php

hiii ,
I want to rewrite the directory www.mysite.com/index.php?category=news
to www.mysite.com/news
I write this code but its doesn't work
anyone can help, please
thanks for help
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?category=$1
Thanks for all who answered my question , all the codes are work when i try to write www.mysite.com/news , but I mean when i click on a link
"a href='index.php?category=news'"link"/a" I want to be rewrite to www.mysite.com/news immediately

Just to confirm, the pattern you want to match is all letters, numbers, hyphens and forward-slashes, right?
If so, try this
RewriteRule ^([a-zA-Z0-9/-]+)$ index.php?category=$1 [QSA,L]
I think the problem may have been your ordering of the hyphen and forward-slash in the character class expression. To match hyphens, they should appear first or last in the set of characters.

Does this work?
RewriteBase /
RewriteRule (.*) index.php?category=$1 [QSA,NC,L]

try this
Options +FollowSymLinks
RewriteEngine on
RewriteRule news/ index.php?category=news
RewriteRule news index.php?category=news

RewriteRule ^(news|or|some|other|category)$ index.php?category=$1 [QSA,NC,L]

Related

Rewrite URL to hide ?get= in .htaccess

This is the structure of my website:
website.com/events/beachparty?date=1224
website.com/events/poolparty?date=0101
website.com/events/boatparty?date=1105
There are lots of different pages all of which I would like to use the get feature on.
I want to rewrite the URL using htaccess so that they can be loaded like this:
website.com/events/beachparty/1224
website.com/events/poolparty/0101
website.com/events/boatparty/1105
Is this possible without having to create a separate rule for each page?
Thanks in advance!
Is this possible without having to create a separate rule for each
page?
Yes, you can use a regex, i.e.:
RewriteEngine On
RewriteRule ^events/page([0-9]+)/([0-9]+)/?$ /events/page$1?date=$2 [L]
The above will rewrite:
www.yoursite.com/events/page99/123
to
www.yoursite.com/events/page99?date=123
Notes:
([0-9]+) will match 1 or more (+) digits
The last forward slash is optional /?
$ means the end of the line (url)
[L] = Last, apache will stop processing further rules
Update based on your comments:
RewriteEngine On
RewriteRule ^events/([0-9a-zA-Z]+)/([0-9]+)/?$ /events/$1.php?date=$2 [L]
RewriteRule ^events/([0-9a-zA-Z]+)/?$ /events/$1.php [L]
[0-9a-zA-Z] - will match any digit or letter from a to z or A to Z, 1 or more times
Try...
RewriteEngine On
RewriteRule ^events/beachparty/([0-9]+)/?$ /events/beachparty?date=$1 [L]
RewriteRule ^events/poolparty/([0-9]+)/?$ /events/beachparty?date=$1 [L]
RewriteRule ^events/boatparty/([0-9]+)/?$ /events/beachparty?date=$1 [L]

I need a regular expression to my htaccess

I'm very new to regular expression and I need to do a redirecting on my .htacess for some urls
Some examples are:
/lentes-de-contato/9/lentes-de-contato-biofinity-coopervision
/lentes-de-contato/9/lentes-de-contato-biofinity-teste
/lentes-de-contato/9/lentes-de-contato-biofinity
/lentes-de-contato/9/biofinity
The regex needs to match the word biofinity but don't match the word coopervision. I tried several ways to build an expression that looks like this: "biofinity" AND !"coopervision" but nothing seems to work. Till now I just have:
/lentes-de-contato/([0-9]+)/(.*biofinity.*)
Could anyone help me?
Maybe, so. Rewrterule will be achieved with your condition
RewriteCond %{REQUEST_URI} biofinity
RewriteCond %{REQUEST_URI} !coopervision
RewriteRule
You can do this in RewriteRule itself using negative lookahead:
RewriteRule ^lentes-de-contato/(\d+)/(?!.*?coopervision).*?-biofinity ... [L,NC]

why this .htaccess regular expression is not working?

i have a regular expression as below
http://www.abc.com/signup.php?id=2
RewriteRule ^signup/([a-z]+)/([a-z]+)$ /signup.php?$1=$2 [L]
but this is not working? can any one help me.is there any thing to enable in the .htaccess file?
As you are passing also numbers, you should match them in regular expression as well:
RewriteRule ^signup/([a-z0-9]+)/([a-z0-9]+)$ /signup.php?$1=$2 [L]
You might want to include other chars as well or rather use metaclass such as \w.
After edits, try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=(\d+)
RewriteRule ^signup\.php$ /signup/%1 [L,R=301]
RewriteRule ^signup/([0-9]+)$ /signup.php?id=$1 [L]
That'll redirect /signup.php?id=123 to /signup/123 which will then be rewritten back to /signup.php?id=123 (but not redirected).

mod rewrite, htaccess and gettign the values

the incoming URL
domain.com/12/3
should be re-written to
domain.com/?w=12&h=3
htaccess
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?w=$1&h=$2 [QSA]
the php
<?php
echo $_GET['h'];
?>
Result
404 page
I've tried using htaccess to change the result of the url and then retrieve the value from the URL, could someone help me out?
Maybe you need to escape - like:
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_\-]+)$ index.php?w=$1&h=$2 [QSA]
PS. I hope the above URL /12/3 is just for example because your regex accepts only a-z
RewriteEngine On
RewriteRule ^[a-z]{2,2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Essentially, I'm pretty sure you need a / in front of index.php
I like to use this generator for my mod-rewrite rules.
http://www.generateit.net/mod-rewrite/
Try changing your rewriterule to the following:
RewriteEngine on
RewriteRule ^([a-z0-9]{2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Your example has two digits as the first parts of the path, which won't be matched by [a-z].
RewriteEngine On
RewriteRule ^(.*)/(.*)$ index.php?w=$1&h=$2 [QSA]
The above sorted it. Thanks for the replies

simple htaccess redirect

It's a part of my htaccess:
RewriteRule ^post/(.*)$ post.php?name=$1
RewriteRule ^(.*)/$ cat.php?name=$1
The URI will be somthing like this:
www.domain.com/category-name/
www.domain.com/post/hello-world
as you see, in the end of the first address (category) there is a '/' and on the second there isn't '/', how can I do it too on the second address? if I will do somthing like this:
RewriteRule ^post/(.*)/$ post.php?name=$1
it won't work because the server 'thinks' that I mean to category address.
hope you understand thank you.
Use the [L] modifier on every rule so it stops processing further rules when a rule matched. In this case you can even make the trailing slash optional!
RewriteRule ^post/(.*)$ post.php?name=$1 [L]
RewriteRule ^(.*)/?$ cat.php?name=$1 [L]
Try using (.*?) instead of (.*).
Without the question mark, it's "greedy" in that it will match everything up to the final match on the line. With the question mark, it will only match up to the first match on the line.
Make the slash optional (both times):
RewriteRule ^post/(.*?)/?$ post.php?name=$1 [L]
RewriteRule ^(.*?)/?$ cat.php?name=$1 [L]

Categories