Rewrite URL to hide ?get= in .htaccess - php

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]

Related

.HTACCESS adding to slugs

So, I'm currently building a REST API in PHP.
I managed to get slugs working for the most part.
If I request /api/admin/v1/users/1, it will return the user I need.
However, I also need to be able to add to it, e.g. /api/admin/v1/users/1/keys.
The HTACCESS file managing the slug is in the folder itself (/users/).
RewriteEngine On
RewriteRule ^(.*)$ user.php?slug=$1 [L]
I tried adding another line, but I think I messed up (I'm not that advanced with HTACCESS)
RewriteRule ^(.*)/keys$ keys.php?slug=$1 [L]
This didn't do anything, it still returns the user object.
RewriteRule ^(.*)$ user.php?slug=$1 [L]
RewriteRule ^(.*)/keys$ keys.php?slug=$1 [L]
The first rule matches everything, so the second rule is never processed. But since the first rule matches everything it will also rewrite itself (to user.php?slug=user.php) on the second pass by the rewrite engine.
You can resolve these issues by making the regex more restrictive. From your example URL it looks like the slug is numeric - in which case you can restrict the regex to match digits (0-9) only.
For example:
RewriteRule ^(\d*)$ user.php?slug=$1 [L]
RewriteRule ^(\d+)/keys$ keys.php?slug=$1 [L]
Note that the first rule also matches an empty URL-path, ie. no slug at all (as does your original rule). The second rule does not permit an empty slug (it would never match anyway).
The second rule don't work because the L flag stay for: last - stop processing rules
So you need to edit to:
RewriteEngine On
RewriteRule ^(.*)$ user.php?slug=$1 [QSA, L]
RewriteRule ^(.*)/keys$ keys.php?slug=$1 [QSA, L]

Rewrite rule not working on localhost

I want to rewrite the url
http://localhost/sample/eventcentre.php?url=someurl
to
http://localhost/sample/vn/someurl
Here is my current rewrite rule:
RewriteEngine On
RewriteRule ^vn/([^/\.]+)?$ /eventcentre.php?url=$1 [L]
it doesn't seem to be working. What could I be doing wrong?
In the rewrite rule you've listed here, it starts with a ^ - this denotes the start of the rewrite.
The example urls you have start with sample - so if you amend the two together it should work ie:
RewriteEngine On
RewriteRule ^sample/vn/([^/\.]+)?$ /eventcentre.php?url=$1 [L]

PHP .htaccess RewriteRule allow multiple combination

I have a problem with my .htaccess file.
In my .htaccess rules if I put this:
RewriteRule ^post/([^/]*)$ /the_post.php?id=$1 [L]
I'll be able to navigate url like this:
http://www.example.com/post/12
But if I try:
http://www.example.com/post/12/
or
http://www.example.com/post/12/something-else-here
The page was not founded.
What is the right way to allow any possible combinations of url?
http://www.example.com/post/12
http://www.example.com/post/12/
http://www.example.com/post/12/something-else-here
Thanks for your time!
With your current rule :
RewriteRule ^post/([^/]*)$ /the_post.php?id=$1 [L]
You can't have any / after post/ in your url. [^/]* means any character except /.
You can try this rule :
RewriteRule ^post/([0-9]+) /the_post.php?id=$1 [L,QSA]
You can use this rule to replace your rule:
RewriteRule ^post/([^/]+)(/[^/]*)?/?$ the_post.php?id=$1 [L,NC,QSA]

.htaccess RewriteRule change underscore to dash

I have a ReWrite rule in my .htaccess file right now:
RewriteRule ^item/?([a-zA-Z0-9_]+)?/?([a-zA-Z0-9_]+)?/?$ static/items.php?a=$1&b=$2 [NC,L]
It will pick any items that have an underscore:
item/new_item/new_order
However, I need to change from underscore to dashes to make it:
item/new-item/new-order
If I simply make a change in RewriteRule string it breaks it. Not sure how to fix that.
RewriteRule ^item/?([a-zA-Z0-9-]+)?/?([a-zA-Z0-9-]+)?/?$ static/items.php?a=$1&b=$2 [NC,L]
This is fairly tricky stuff but can be done using following Rewrite rules:
RewriteEngine On
# first replace each _ by - recursively
RewriteRule ^(item)/([^_]*)_(.*)$ /$1/$2-$3 [L,NC]
# now usual stuff to forward request to static/item.php
RewriteRule ^(item)/([^_/]*)/?([^_/]*)/?$ static/$1.php?a=$2&b=$3 [L,QSA,NC]

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