.htaccess RewriteRule change underscore to dash - php

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]

Related

Replace %20 in url with "_" .htaccess

How can i rewrite the url further for %20 to be replaced with _ .
Here is my current url:
localhost/car-86-Honda%20Accord%202019
here is the code i have used before.
RewriteRule ^car-([^/]+)-([^/]+)$ ncarposts.php?carpostid=$1&title=$2
I have tried searching but there's no solution for this common issue we have used in our urls...
complete code is:
# Rewrite for ncarposts.php?carpostid=83&?title=Toyota%20Land%20Cruiser%202019
RewriteRule ^car-([^/]+)-([^/]+)$ ncarposts.php?carpostid=$1&title=$2
First, you need to know the the RewriteRule matches with decoded url, so you may try this code:
RewriteEngine On
RewriteRule "^(\S*)\s+(\S*)$" /$1_$2 [L,NE,R=302]
RewriteRule "^(\S*)\s+(\S*\s+.*)$" $1_$2 [L]
# remove multiple underscore
RewriteRule ^(.*)_{2,}(.*)$ /$1_$2 [L,R=302]

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]

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]

Could i have 2 links from the same page with htaccess?

So my htaccess lines look like this:
RewriteRule ^meniu/([a-zA-Z0-9]+)/$ produse.php?categorie=$1
RewriteRule ^meniu/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ produse.php?categorie=$1&produs=$2
www.mysite.com/meniu/pizza/ works
www.mysite.com/meniu/pizza/Quatro_Formaggi/ doesn't work, it displays 404 not found.
Your URL has the underscore character
www.mysite.com/meniu/pizza/Quatro_Formaggi/
so just add the _ to the RewriteRule to match it
RewriteRule ^meniu/([a-zA-Z0-9]+)/$ produse.php?categorie=$1
RewriteRule ^meniu/([a-zA-Z0-9]+)/([a-zA-Z0-9_]+)/$ produse.php?categorie=$1&produs=$2
Your URL has a - (underscore character); and your rules don't; so you need to add the _ to the rule.
Also instead of using [a-zA-Z0-9] I would suggest using [a-z0-9] and the Not Case Sensitive flag ([NC]). So My suggested rules would be:
RewriteRule ^meniu/([a-z0-9]+)/$ produse.php?categorie=$1 [NC]
RewriteRule ^meniu/([a-z0-9]+)/([a-z0-9_]+)/$ produse.php?categorie=$1&produs=$2 [NC]
Also make sure you have a rule to add trailing slashes above this one or it will be annoying to any users hand entering the address to remember to have the trailing slash.

rewrite a folder so all files display under new rewrite rule

Is there a way that I can rewrite a folder so that all the files under that folder follow the same rule? For example:
if i have a folder with say 5 php files (a.php, b.php, c.php, d.php, index.php) in it and i use the following rule:
RewriteRule ^products/storage/?$ /content/products/storage/index.php [QSA,L]
is there a way that I can get all the files to show to be accessed like: site.com/products/apples/a.php, site.com/products/apples/b.php, etc. without having to write a rule for each one?
I tried the following but it didnt work.
RewriteRule ^products/storage/?$ /content/products/storage/ [QSA,L]
I also need it to NOT overwrite my other rules such as:
RewriteRule ^products/storage/?$ /content/products/storage/product-name1/ [QSA,L]
RewriteRule ^products/storage/?$ /content/products/storage/product-name2/ [QSA,L]
any ideas?
Your problem is the trailing $ on the end of the regex. This will only allow a match if the full URI matches products/storage (with optional trailing slash) exactly. Instead, try the following and note the absence of the trailing $ character:
RewriteRule ^products/storage/? /content/products/storage/ [QSA,L]
This will match anything that starts with products/storage (with optional trailing slash). Alternatively, if you wanted to capture and re-use everything in the URI that followed products/storage/ you could try:
RewriteRule ^products/storage(/?.+)?$ /content/products/storage$1 [QSA,L]
UPDATE
Should you need to preserve other RewriteRules as your updated question suggests, you should look to add a RewriteCond condition like so:
RewriteCond !^products/storage/?$
RewriteRule ^products/storage(/?.+)?$ /content/products/storage$1 [QSA,L]
The RewriteCond tells the RewriteRule to only process if the condition is not met.

Categories