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]
Related
Hello i am trying to change %20 to - using htaccess file but i am not being able to do it. Please have a look below code.
# Turn Rewrite Engine On
RewriteEngine on
# Rewrite for index.php
RewriteRule ^home index.php [L]
#Rewrite for inner.php?u=####
RewriteRule ^user/([0-9a-zA-Z]+) inner.php?u=$1 [L]
#Rewrite for article.php?id=1&title=Title Goes Here (unlimited number of space can be here)
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+) article.php?id=$1&title=$2 [L]
and i am trying to get the value in article.php and echo the value using php like below
<?php echo $_GET['title'] ?>
The url looks like below. Their is %20 in the url but i want to convert it in -
http://localhost/learning/article/1/i%20am%20title
And when i try to echo the title value it only display i other are not shown. Please help me to fix it. Thank you in advance. :)
Please try with below .htaccess code. ([^/]+) REGEX code will allow all the chars in the querystring. So it will allow spaces and your querystring value will not broke down up to first space now.
# Turn Rewrite Engine On
RewriteEngine on
# Rewrite for index.php
RewriteRule ^home index.php [L]
#Rewrite for inner.php?u=####
RewriteRule ^user/([^/]+) inner.php?u=$1 [L]
#Rewrite for article.php?id=1&title=Title Goes Here (unlimited number of space can be here)
RewriteRule ^article/([0-9]+)/([^/]+) article.php?id=$1&title=$2 [L]
Apache automatically rewrites %20 into space (\s) but your rule doesn't accept it. This should work:
RewriteRule ^article/(\d+)/([0-9a-zA-Z_-\s]+) article.php?id=$1&title=$2 [L]
In your PHP code you will may need use urldecode(). However, I highly recommend to ignore title in GET parameter and to use only ID (don't forget to check if it is the right type (eg. integer)). Reading the title from your database is more secure.
How can I show following URL using .htaccess re-write rule with a non ? parameter?
Original URL
http://example.com/index.php?store=1
Desired URL
http://example.com/1/
OR
Desired URL
http://example.com/store/1
I am trying following code in .htaccess. What I am missing here?
RewriteEngine On
Options +Followsymlinks
RewriteCond %{REQUEST_FILENAME} !-f
rewriteRule ^store/(.+)\$ index.php?store=$1 [L]
my index.php file has this code
<?php
$storeid = $_GET['store'];
echo $storeid;
?>
What I am missing here?
You are escaping your end-of-string character, causing the expression to look for a literal $ sign so it never matches.
You need something like:
rewriteRule ^store/(.+)$ /index.php?store=$1 [L]
^ no back-slash here
or if you want to make sure you only match numbers:
rewriteRule ^store/(\d+)$ /index.php?store=$1 [L]
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 -.
I am trying to re-write the dynamically generated URL. But the rules i have written is not affecting the URL even a single bit.
The URL currently shows up like: http://ukfurniturespecialist.co.uk/county.php?c=Oxfordshire&%20t=Faringdon
And i would like it to look like: http://ukfurniturespecialist.co.uk/county-c-Oxfordshire-t-Faringdon.html
This is what i have tried:
Options +FollowSymLinks
RewriteEngine on
RewriteRule county-c-(.*)-%20t-(.*)\.html$ county.php?c=$1&%20t=$2
Any help is much appreciated,Thanks.
For your URL following rule should work:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+county\.php\?c=([^&]*)&.*?t=([^&\s]*) [NC]
RewriteRule ^ county-c-%1-t-%2.html? [R=302,L]
RewriteRule ^county-c-([^-]+)-t-([^.]+)\.html$ county.php?c=$1&\%20t=$2 [L,QSA,NE]
%20 represents a space character in url. when writing rewrite rule the url is already decoded and handled using "\ " instead of %20
For your URL the rewrite rule should be
Options +FollowSymLinks
RewriteEngine on
RewriteRule county-c-(.*)-\ t-(.*)\.html$ county.php?c=$1&\ t=$2
I dont think you need to put space character in your url. If you remove the %20 from your URL the rewrite rule should be
RewriteRule county-c-(.*)-t-(.*)\.html$ county.php?c=$1&t=$2
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]