htaccess rewriterule not working when value contains hyphen - php

I have following rewriterule:
RewriteEngine On
RewriteRule ^(.*)-(.*)\.html$ $1.php?filter=$2 [QSA]
RewriteRule ^(.*)\.html$ $1.php
So example.com/folder/index.php becomes example.com/folder/index.html
and
example.com/folder/index.php?filter=value becomes example.com/folder/index-value.html
It works, echo $_GET["filter"] outputs value
But when I try example.com/folder/index-value-two.html to output value-two I get error:
Not Found
The requested document was not found on this server.
What is the problem here?

Your RewriteRule is splitting on the second hyphen, not the first one, and so the request it is trying to make is example.com/folder/index-value.php?filter=two, which, since index-value.php does not exist, gives you a 404 error. Try using this instead:
RewriteEngine On
RewriteRule ^([^\-]*)-(.*)\.html$ $1.php?filter=$2 [QSA]
RewriteRule ^(.*)\.html$ $1.php
By changing . to [^\-] you match any character except a hyphen with the first group, instead of any character. This should split the URL at the first -.

Related

Redirect rule to php query but remove any query string in the requested url

My url is being redirected using .htaccess as follows:
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
Friendly url -> translates to php url
domain.com/b/hello/2 -> b/view.php?id=2&name=hello
BUT when someone comes to the site as follows:
domain.com/b/hello/2?query=xyz
I don't know how to get rid of the ?query=xyz
I have tried everything including [QSD] and I can't seem to get it to work.
Update
I have managed to get it to work with the following but it does two 301 redirects instead of one:
RewriteCond %{THE_REQUEST} \?[^\ ]+
RewriteRule ^b/(.*)$ /x/$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)/([^/]+)?$ b/view.php?id=$2&&name=$1
Check if adding the question mark at the end of the rule will cancel appending query string from left side
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1? [QSD,L]
You can use an additional Rule to catch for GET parameters and strip them off.
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
To make it only work on the /b/ subfolder, use this:
RewriteCond %{QUERY_STRING} .+
RewriteRule ^b/(.*)$ b/$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
The first rule will redirect everything that matches your rule to the URL without any GET parameters (note the ? at the end of the rewrite rule, it will strip off the parameters).
The second rule will match in the case the first rule cannot be applied, i.e., when there are no paramaters

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]

URL Rewriting .php to .html

I'm converting my urls extension from .php to .html in my .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} (.).php
RewriteRule ^(.*).php $1.html [R=301,L]
RewriteRule ^(.*).html $1.php [L]
FallbackResource /index.php
The problem is that I have some section with the word "php" on it:
www.mywebsite.com/phpscripts.php
And when it is converted:
www.mywebsite.com/htmlscripts.html
^(.*).php
This regex says: anything, including nothing, followed by a single arbitrary character, followed by "php". For example, it'll match "blahphp.html", specifically it'll match the "blahphp" part and not care about the extension at all.
What you're looking for is this:
RewriteRule (.+)\.php$ $1.html [R=301,L]
RewriteRule (.+)\.html$ $1.php [L]
(.+)\.php$ is something (at least one character) followed by a period followed by "php" at the end of the string. You can also get rid of the RewriteCond, it doesn't add anything to these rules.
Also note that you should be changing all your HTML files to link to href="...html". Don't rely on these redirects alone to fix your problem; not only is it inefficient to redirect every single request, it'll also break POST requests. It's only acceptable to redirect clients which try to open the old URLs for whatever reason to the new canonical URLs.

Trouble when writing RewriteRule in apache

My current url is like news/sometopics/newstitle.html.
And I want url like this sometopics/newstitle.html.
Why is the following not working?
RewriteRule ^(.+/)(.+).html$ news/$1/$2.html
By the way, I am using GoDaddy Apache server. This rule:
RewriteRule ^search/$ admin/plus/search.php
works fine.
The first rule will match only alphanumeric strings such as: titlenews123. I prefer it over the generic match everything meta-char (.+)
RewriteRule ^([0-9a-z-]+)/([0-9a-z-]+).html$ news/$1/$2.html [L]
If your sometopics and newstitle are not only alphanumeric you can do (discouraged)
RewriteRule ^([^/]+)/([^/]+).html$ news/$1/$2.html [L]

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