HTaccess 404 with mod_rewrite - php

I've got a small problem with my HTaccess urls. I wanted to make my URLs more SEO friendly, my current URL is:
url.com/blogitem/2
url.com -> Domain ; blog -> blogitem.php ; 2 -> ID for the blog item to get from the DB
Now I would like the URL to be like this:
url.com/blogitem/2/name-title-info
I've made the HTaccess part and the PHP part for it, but it will give me a 404 error page in return every time I visited the URL.
The HTaccess part:
# Blog SEO Urls.
RewriteRule ^blog/([0-9]*)/?$ blog.php?page=$1 [NC,L]
RewriteRule ^blogitem/([0-9]*)/?$/([a-z]*)/?$ blogitem.php?id=$1&name=$2 [NC,L]
The PHP part:
$trancTitle = trim(strtolower($blogTitle));
$underscoreTitle = preg_replace('/\s+/', '-', $trancTitle);
<a href="blogitem/<?php echo $blogId; ?>/<?php echo $underscoreTitle;?>">
Can someone explain why I'm getting an error page in return(404 to be exactly). I'm sorry for my bad English, it isn't my mother tongue. Thanks for helping in advance!

Your regex doesn't seem right, try these 2 rules:
RewriteEngine On
RewriteBase /
# Blog SEO Urls.
RewriteRule ^blog/([0-9]+)/?$ blog.php?page=$1 [NC,L,QSA]
RewriteRule ^blogitem/([0-9]+)/([\w-]+)/?$ blogitem.php?id=$1&name=$2 [QSA,NC,L]

The url that you are showing does not have "/" at the end: url.com/blogitem/2/name-title-info
Then you have mentioned blog.php in your .htaccess I think you are wanting to have this url too url.com/blog/2
So, you need to change a little bit you regular expression, try to putting this in your .htaccess:
# Blog SEO Urls.
RewriteRule ^blog/([0-9]+)$ blog.php?page=$1 [L]
RewriteRule ^blogitem/([0-9]+)/([a-z]+)$ /blogitem.php?id=$1&name=$2 [L]
Then in your php you will receive the variables via GET method
Try this to see that:
<?php
echo '<pre>';
print_r($_GET);
echo '</pre>';
?>

Related

htaccess Working fine for all but few links not working

For my work portfolio i am using htaccess for seo friendly URL and its working fine for all but for two projects its not working and going to another page (rs.php) and redirecting to home page.
Site is https://www.rsseosolution.com/seo-work-portfolio.php
At this page all projects are opening fine but Number 4 (Fair Price Movers) and number 11 (New Hampshire Lawyers) ..... these two are not opening while URL and process is same.
page name is case-studies.php
I tried by add "echo and die option" at starting of page
echo "Testing value is coming on not"; die();
But as i said that its going to another page rs.php
Using htaccess as below for our portfolio project details at case-studies.php
Options +FollowSymLinks
RewriteEngine on
RewriteRule case-studies/(.*)-(.*)\.php$ case-studies.php?project=$1&id=$2
RewriteRule package/(.*)-(.*)\.php$ package.php?name=$1&id=$2
RewriteRule rs-(.*)\.php$ rs.php?cid=$1
I am not getting that why only two projects URL is going to rs.php.
What am i missing? If need any other details then please let me know.
Hope for quick response.
The error happen in this url fair-price-movers-164 as it contain rs-164 which will match the last rule.
You need to add [L] at the end of each rule to stop matching.
Options +FollowSymLinks
RewriteEngine on
RewriteRule case-studies/(.*)-(.*)\.php$ case-studies.php?project=$1&id=$2 [L]
RewriteRule package/(.*)-(.*)\.php$ package.php?name=$1&id=$2 [L]
RewriteRule rs-(.*)\.php$ rs.php?cid=$1 [L]

How to set .htaccess for 2 type get value on the same page?

For this page test.php
i have to use 2 format url
www.example.com/test/1234
and
www.example.com/test/1234/abcd
My .htaccess is
RewriteRule ^test/([^-]*)/([^-]*)$ /test.php?one=$1&two=$2 [L]
It's work good with www.example.com/test/1234/abcd but redirect internal error with www.example.com/test/1234
I want to know how can i edit .htaccess for work good with my 2 url format on test.php ?
Try the following instead:
RewriteRule ^test/([^-]*)$ /test.php?one=$1&two=$2 [L]
Just remove the 2nd /([^-]*) it is making no difference, as you can see here. Removing it will allow for you to access www.example.com/test/1234 too.
You can use:
RewriteRule ^test/([^/-]+)(?:/([^-]*))?$ /test.php?one=$1&two=$2 [L]

.htaccess redirect subfolders

Im struggling with a .htaccess settings to get following:
To create Multi-language functionality in my CMS without duplicating the productdatabase I would like to get the content of the default language, but keeping the right url in the addressbar.
For example:
http://www.myshop.com/en/webshop get the content of http://www.myshop.com/webshop
http://www.myshop.com/en/collection get the content of http://www.myshop.com/collectie
And also for the products:
http://www.myshop.com/en/webshop/item1 get content of http://www.myshop.com/webshop/item1
http://www.myshop.com/en/collection/product1 get the content of http://www.myshop.com/collectie/product1
In short:
In case of calling the folders '/en/webshop/' and '/en/collection/' there must be a rewrite to '/webshop' and '/collectie'.
Anyone got a clue?
Thanks in advance...
What about:
RewriteEngine on
RewriteRule ^/?en/webshop/(.*)$ /webshop/$1
RewriteRule ^/?en/collection/(.*)$ /collectie/$1
The first line just enables mod_rewrite
The second line (1st rule) matches anything under /en/webshop and discards the /en
The third line is similar to the second but it also "renames" collection to collectie
If you have more folders/paths you want to redirect I 'd suggest adding the rule:
RewriteRule ^/?en/(.*)$ /$1
which is rewriting anything that starts with /en.
Hope it helps, note it is not tested
Use below rule:-
RewriteEngine on
RewriteRule ^en/(.*)$ /$1 [L,R=301,QSA]
OR
RewriteEngine On
RewriteRule ^(.*)/en/(.*)$ $1/$2 [R=301,L]
Hope it will help you :)

url rewrite not reading in php

I am beginner in url rewriting and i have studied about it from various sources.Everything seems to be working till now but this is just not working.
I have this line of code in my htaccess file.
RewriteRule ^notice/all/page/([0-9]+)/?$ files/all_notice.php?show=all&page=$1 [NC,L]
This is the url i am visiting
http://localhost/notice/all/page/1
and when reading variables in php like this-
<?php
$type=$_GET['show'];
$page=$_GET['page'];
echo $type.'--'.$page;
?>
This is what i get
all--notice
I am getting the show variable as it is but instead of page number i am getting notice.
What is going wrong?
Your RewriteRule is this:
RewriteRule ^notice/all/page/([0-9]+)/?$ files/all_notice.php?show=all&page=$1 [NC,L]
Try this:
RewriteRule ^notice/all/page/([0-9]+)/?$ /files/all_notice.php?show=all&page=$1 [L]
The only difference is the addition of a / in front o files/… and removing the NC so it is only [L].
EDIT: And perhaps you should see what happens if you change that $1 to $2, $3 or even $4 to see if any of those values get passed through.
Try this:
RewriteRule ^notice/all/page/([0-9]+)?$/ files/all_notice.php?show=all&page=$1 [NC,L] //for urls like localhost/notice/all/page/1/
RewriteRule ^notice/all/page/([0-9]+)?$ files/all_notice.php?show=all&page=$1 [NC,L] //for urls like localhost/notice/all/page/1
You have ?$ after the / but it should be after the regex class

#HTACCESS hide multi PHP GET tag from the URL

i'm replacing this
localhost/load.php?code=foo
with this
localhost/foo
using this htaccess code
RewriteRule ^(\w+)$ ./load.php?code=$1
works pretty good! but what about including other get tag in the same URL like
localhost/load.php?user=foo&code=foo
replaced it with htaccess to be like
RewriteRule ^(\w+)$ ./load.php?user=$1&code=$1
but doesn't work as well! so what is the correct htaccess code to do this?
Depending on your other RewriteRules you might want to use a catch-all rule with the QSA (QueryStringAppend) flag.
The QueryString is the part after ? so user=foo&code=foo.
If you have a rule that says:
RewriteRule ^(.*)$ load.php?__path=$1 [QSA]
And you call:
my.domain.com/page/sub?foo=bar&baz=ipsum
load.php will get the following GET:
__path = page/sub
foo = bar
baz = ipsum
With a rule like this you can handle any URL.

Categories