Query string url to seo friendly url - php

i am trying to rewrite a URL for SEO purpose.
The old URL is:
http://www.example.com/recipe_local.php?hl_cusine=1
The new URL should be like bellow and automatic redirect to this url if user come above url
http://www.example.com/recipes/healthy-recipes
My Code in the .htaccess is:
RewriteEngine on
RewriteRule ^recipes/healthy-recipes/$ recipe_local.php?hl_cusine=$1 [NC,L]
RewriteRule ^recipes/healthy-recipes$ recipe_local.php?hl_cusine=$1 [NC,L]
Even after hours of research, i have no clue why this is not working :(

The $1 in your rewrite is pointing to a backreference you never capture.
Unless you have a RewriteCond you are not showing?
try:
RewriteEngine on
RewriteRule ^recipes/healthy-recipes/$ recipe_local.php?hl_cusine=1 [NC,L]
RewriteRule ^recipes/healthy-recipes$ recipe_local.php?hl_cusine=1 [NC,L]

Related

.htaccess SEO Friendly URLs not working with two parameters

I am trying to create SEO friendly URLs for my site and I have come to problem that I can´t solve. I can´t figure out how to setup the rewrite rule so it will show for example like this www.mysite.com/shows/late-night and www.mysite.com/news/title-of-article. Here is my htaccess file:
RewriteEngine on
RewriteRule ^(.*)/([a-z_-]+) index.php?switch=$1&shows=$2 [NC,L]
RewriteRule ^(.*)/([a-z_-]+) index.php?switch=$1&article=$2 [NC,L]
RewriteRule ^shows/$ index.php?switch=shows [NC,L]
RewriteRule ^articles/$ index.php?switch=article [NC,L]
The rewrite rule works if I comment out the first and the third line, but how I do so both will work.
1st and 2nd rule cannot coexist because they are matching same URI pattern. Only first will all the time. Tweak your rules like this:
RewriteEngine on
RewriteRule ^(shows)/([a-z_-]+)/?$ index.php?switch=$1&shows=$2 [NC,L,QSA]
RewriteRule ^(news)/([a-z_-]+)/?$ index.php?switch=$1&article=$2 [NC,L,QSA]
RewriteRule ^(shows|news|articles)/?$ index.php?switch=$1 [NC,L,QSA]

How can I redirect an old long URL to a new SEO friendly URL?

I'm trying to redirect an old URL :
www.mydomain.com/prodotti/G/YToyOntzOjg6IklEX01BUkNBIjtzOjI6IjI3IjtzOjEwOiJJRF9NT0RFTExPIjtzOjM6IjU2OCI7fQ..
to this one , shorter and SEO friendly:
www.mydomain.com/moto/aprilia/caponord_1200
I'm doing this writing a 301 redirect on htaccess, this is the line that I use:
Redirect 301 /prodotti/G/YToyOntzOjg6IklEX01BUkNBIjtzOjI6IjI3IjtzOjEwOiJJRF9NT0RFTExPIjtzOjM6IjU2OCI7fQ.. http://www.mywebsite.com/moto/aprilia/caponord_1200
But when i insert on the browser the old URL the redirect result is :
www.mywebsite.com/moto/aprilia/caponord_1200?/prodotti/G/YToyOntzOjg6IklEX01BUkNBIjtzOjI6IjI3IjtzOjEwOiJJRF9NT0RFTExPIjtzOjM6IjU2OCI7fQ..
What's is wrong in my 301 redirect? how can I obtain a pure redirect without URLs merging?
Thanks
this is the htaccess with a modrewrite test
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|media|js|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteRule ^/moto/aprilia/capondor_1200$ /prodrotti/G/YToyOntzOjg6IklEX01BUkNBIjtzOjI6IjI3IjtzOjEwOiJJRF9NT0RFTExPIjtzOjM6IjU2OCI7fQ.. [R=301, L]
Your htaccess should look like this
RewriteEngine on
RewriteRule ^prodrotti/G/YToyOntzOjg6IklEX01BUkNBIjtzOjI6IjI3IjtzOjEwOiJJRF9NT0RFTExPIjtzOjM6IjU2OCI7fQ\.\.$ /moto/aprilia/capondor_1200? [R=301,L]
RewriteCond $1 !^(index\.php|images|css|media|js|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Try a rewrite rule like this
RewriteEngine On
RewriteRule ^/moto/aprilia/capondor_1200$ /prodrotti/G/YToyOntzOjg6IklEX01BUkNBIjtzOjI6IjI3IjtzOjEwOiJJRF9NT0RFTExPIjtzOjM6IjU2OCI7fQ [R=301]
You may need to replace YToyOntzOjg6IklEX01BUkNBIjtzOjI6IjI3IjtzOjEwOiJJRF9NT0RFTExPIjtzOjM6IjU2OCI7fQ in the rewrite rule the whole string, as I assume .. means it's longer than what you provided?

Issue with Rewrite Rule in .htaccess file

I was about to convert my webpage url into seo friendly. My url link is like follows
http://example.com/display.php?id=***
Now i want to convert this into seo friendly url, which could be
http://example.com/partners-id-***.html
Something like above. I used
RewriteEngine On
RewriteRule ^partners-id-([^-]*)\.html$ /display.php?id=$1 [L]
Rewrite rule in htaccess file. But still the url stays same as older one. Anyone can help me to fix this issue???
You need 1 more 301 rule before this rule for external redirection:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+display\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /partners-id-%1.html? [R=301,L]
RewriteRule ^partners-id-([^.]*)\.html$ /display.php?id=$1 [L,QSA]
As commented by Justin and other folks it is better to change your links to /partners-id-123.html. However for links already cached by search engines will be taken care by first rule here which will 301 redirect:
/display.php?id=1234 => /partners-id-1234.html
Please try like below:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /display.php?id=$1 [L]

How exactly does mod_rewrite work when displaying old url

I have used mod_rewrite to change http://mywebsite.com/?page=2 to http://mywebsite.com/page/2 using the following code:
RewriteEngine On
RewriteRule ^page/([^/]*)\.php$ /?page=$1 [L]
But when i type in the old url - http://mywebsite.com/?page=2 the page still appears and the url doesnt change to the static one. Is mod-rewrite meant to redirect the user or just make everything on the dynamic url appear on the static one when entered? If so how can i redirect any user that goes to the old dynamic url - http://mywebsite.com/?page=2 to the new static one http://mywebsite.com/page/2? Plus does google still index the old url?
This is my complete .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# external redirect from actual URL to pretty one
RewriteCond \s/+\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
# existing rule
RewriteRule ^page/([^/]+)/?$ /?page=$1 [L,QSA]
Have your full .htaccess like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
# existing rule
RewriteRule ^page/([^/]+)/?$ /?page=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [L]
mod_rewrite necessarily will not redirect the visitor to the new fancy URL, but it will make sure that anybody who visits the new URL will still access it as if it is using the old URL
I believe you can achieve something like that with mod_substitute, but that's not something I will advice you to do now.
If you can, manually change the links to reflect the new changes, or have a function which will parse out your links such as
http://mywebsite.com/?page=2 to http://mywebsite.com/page/2
such as
function fancyuri($url){
//blah blah, strip out ? and change all occurence of equality sign = to forward slash
//do some other tricks
//return formatted links
}
And then pass your links as a param to the function and have it return your nice urls, in this case, even if you don't want it again, you can strip off the formatting codes in the function and still have ur original url returned.

get page no from url using htaccess

I want to get page no from url
Eg:
www.example.com/education/tutorial/php-234.html
to
PageNo=234
and
www.example.com/course-1504.html
to
PageNo=1504
I am using the following code in .htaccess
RewriteEngine on
RewriteRule ^([0-9]+).html$ Pages.php?PageNo=$1 [NC,L]
But it is not working
If we use
RewriteRule ([0-9]+).html$ Pages.php?PageNo=$1 [NC,L]
I got page no, But
RewriteRule ^([a-zA-Z0-9-]+).html$ Pages.php?PageName=$1 [NC,L]
is not working
Any suggestion for both combination
try:
RewriteEngine on
RewriteRule ^(.*)-([0-9]+)\.html$ Pages.php?PageName=$1&PageNo=$2 [L,QSA]

Categories