Simple mod rewrite query string but still failed - php

I want to create .htaccess mod rewrite but still have problem.
Suppose i created friendly URL like this :
mydomain.com/mypage/12345/title-of-news
I want that friendly URL process the below URL in hidden process :
mydomain.com/index.php?p=mypage&idnews=12345
Values of "p" and "idnews" are dynamic, so they will have different value.
I tried below code but still didn't work. Anything wrong?
RewriteBase /
RewriteCond %{QUERY_STRING} p=(\w+)&idnews=(\d+)
RewriteRule ^index.php /%1/%2 [L,R=301]
Any help would be appreciated. Sorry if this is duplicated question, if don't mind please tell me the answer link. Thanks a lot.

I think you don't need the RewriteCond you write there.
And you can use following rule
RewriteRule ^/?([0-9a-zA-Z_.-]+)/([0-9]+)/([0-9a-zA-Z_.-]+)$ index.php?p=$1&idnews=$2 [L]

If you want to change index.php?p=foo&idnews=bar to /foo/bar , try theses rules :
RewriteEngine on
##externally redirect "/index.php?p=foo&idnews=bar" to "/foo/bar"##
RewriteCond %{THE_REQUEST} /index\.php\?p=([^&]+)&idnews=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [L,R,NC]
##internally redirect "/foo/bar" to "/index.php?p=foo&idnews=bar"##
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?(.*)?$ /index.php?p=$1&idnews=$2 [NC,L]

Related

rewrite url with parameters in .htaccess

I have this incoming url where the last part is dynamic.
https://www.rishi.com/q/CuYuGy
and want to rewrite the url to below link.
https://rishi.com/test.php
where it gets the dynamic part and performs action
so far i have done this
RewriteCond %{HTTP_HOST} ^rishi.com/q/%{QUERY_STRING}$
RewriteRule ^$ {HTTP_HOST} ^rishi.com/test.php [L,R=301]
please help me resolve it.
Thanks
Try this
RewriteRule ^q/([a-zA-Z0-9-/]+)$ test.php?query_string=$1
RewriteRule ^q/([a-zA-Z0-9-/]+)/$ test.php?query_string=$1
Try this below htaccess code. It should help you
RewriteCond %{REQUEST_URI} ^/q/(.*)$
RewriteRule ^(.*)$ /test.php [L,R=301]

How to remove extension .php in the middle of URL

Here is my URL:
http://localhost/school-project/project1/mypage.php/home
I wanna get rid of .php in mypage.php. So the new URL should look like this:
http://localhost/school-project/project1/mypage/home
I have tried to use RewriteRule in .htaccess, but none of them worked!
Here is the code in my .htaccess:
(this one actually gets rid of the .php, but it turned the page to Object not found, error 404)
RewriteRule ^mypage.php/(.*)$ http://localhost/school-project/project1/mypage/$1[NC,L,R]
or
RewriteRule (.*)mypage/(.*)$ /mypage.php?/$1 [L]
I don't really know where the problem is. Any ideas?
Thanks!
Try this :
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.*)\.php(.*)\sHTTP.*$ [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_URI} !\.php
RewriteRule ^([^\/]*)/([^\/]*)$ $1.php/$2 [L]
Second & third lines are removing php extension externally.
Forth & fifth to redirect request to original path internally .
Clear browser cache then test , if it is Ok change R=302 to R=301 in order to be permanent redirection .
I found another solution for my problem. Thank you so much for all the help!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^mypage(/.+)$ mypage.php$1 [NC,L]

Dynamic URL rewriting with .htaccess - not rewriting

I am trying to create seo-friendly URLs from my dynamic ones with a .htaccess rewrite. I've tried a ton of different rewrites and none of them are updating the URL. My other URL rewrite condition works fine...removing ".php" from the end of the URL.
Example:
Trying to change this:
food-truck.php?city=miami-fl&name=john doe
To this:
food-truck/city/miami-fl/name/john%20doe
Here is one of the rewrites I have tried with no success:
Options +FollowSymLinks
RewriteEngine on
RewriteRule food-truck/city/(.*)/name/(.*)/ food-truck.php?city=$1&name=$2
Any suggestions appreciated. Haven't been able to figure out a solution from similar questions in Stack Overflow posts or forums.
Thank you.
EDIT: Here is what did the trick...
RewriteCond %{QUERY_STRING} ^city=([^&\s]+)&name=([^&\s]+)$
RewriteRule ^(?:food\.php|)$ /%1?/%2? [R=301,L]
RewriteCond %{QUERY_STRING} ^city=([^&\s]+)&name=([^&\s]+)$
RewriteRule ^(?:food\.php|)$ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\s\/]+)/?$ food.php?city=$1&rname=$2&r [L,QSA]
There is an extra slash in the end of your rule. Try to remove it:
RewriteRule food-truck/city/(.*)/name/(.*) food-truck.php?city=$1&name=$2
Also maybe you need to add the beginning and end:
RewriteRule ^food-truck/city/(.*)/name/(.*)$ food-truck.php?city=$1&name=$2

Magento multi language rewrites SEO fix?

I have made a website in with Magento in one language 2 years ago and now I would like to add another language.
My actual structure is like that
example.com/category/
example.com/about.html
example.com/product.html
and I would like to obtain a structure like this:
example.com/lang1/category/
example.com/lang1/about.html
example.com/lang1/product.html
example.com/lang2/category/
example.com/lang2/about.html
example.com/lang2/product.html
This is not a big deal since Magento allowes me us to do that simply logging in the backend ->System -> Configuration -> Web -> Add store code to Urls (YES)
I can't do this setting right now because before that I need to fix and do rewrites from the first structure to the new one.
example.com/everything_without_the_/lang1/_path to a permanent
example.com/lang1/everything
I need for example that the urls from the backlinks from posts from the socials and other webistes won't get a 404 error but will automatically be redirected with a 301 redirection to the equivalent page in the new structure.
So I would like to add a script like this in the natural language:
rewrite permanently all the urls which ends not with /lang1/ or /lang2/ to urls with the prefix /lang1/
I know that I could manually add rewrite rules with this schema in the Magento backend but I would prefer to know if it is possible to batch this directly from the database or with some script in the .htaccess or index.php and above all which one between these solutions will have a less negative impact in SEO and SERP.
I've found a solution
Hi I've found a solution:
RewriteCond %{HTTP_HOST} ^(.)?example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/it/
RewriteCond %{REQUEST_URI} !^/en/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /it/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.)?example.com$ [NC]
RewriteRule ^(/)?$ it/index.php [L]
at a first look works, but I not guarantee.
I hope this will help
Cheers
Try this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/lang1/
RewriteCond %{REQUEST_URI} !(.*)/lang2/
RewriteRule ^(.*)$ http://www.domain.com/$1/lang1/ [R=301,L]
you can use .htaccess 301 url to make if possible with your own regex function
As you need some specific requirement to do with your language part
here i am giving you category example
Redirecting Subcategory URL with New Parent URL
Examples:
category/sub1 redirect to cat/sub1
category/sub2 redirect to cat/sub2
category/sub3 redirect to cat/sub3
This may be needed if you changed a parent Category URL or even if you want to remove the parent URL completely:
RewriteRule ^category/(.*) http://www.yourwebsite.com/cat/$1 [R=301, L]
OR to remove '/category/' completely from yourwebsite.com/category/sub and end up with yourwebsite.com/sub ('sub' can be any url):
RewriteRule ^category/(.*) http://www.yourwebsite.com/$1/ [R=301, L]
And also you can refer
http://www.learnmagento.org/magento-tutorials/301-redirects-in-magento/
http://blog.maximusbusiness.com/2012/10/magento-url-rewriting-regex-and-301-redirects-tips/
hope this will sure help you.
I've found a solution
Hi I've found a solution:
RewriteCond %{HTTP_HOST} ^(.)?example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/it/
RewriteCond %{REQUEST_URI} !^/en/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /it/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.)?example.com$ [NC]
RewriteRule ^(/)?$ it/index.php [L]
at a first look works, but I not guarantee. I hope this will help Cheers

htaccess multiple query string redirects to different urls

I am totally new to attempting redirects in htaccess but have a list of about 20 urls, all with query strings and all going to different urls.
I have managed to get a redirect using a query string working using RewriteCond and RewriteRule but when I add the other urls in the same format they all seem to redirect to the url in the first RewriteRule.
Its getting so frustrated as I have searched everywhere and tried so many ways to try and get this working. Hopefully someone on here can help me!
Here are a couple of the urls I need to redirect:
/store/index.php?search=flip flops >> http://www.stonemenswear.co.uk/menswear/flip-flops
/store/index.php?search=Boss+Orange+Shorts >> http://www.stonemenswear.co.uk/menswear/shorts
And here is the code I have got so far:
RewriteEngine on
RewriteCond "%{QUERY_STRING} search=flip flops"
RewriteRule ^index\.php$ http://www.stonemenswear.co.uk/menswear/flip-flops/? [R=301,N]
RewriteCond %{QUERY_STRING} search=Boss+Orange+Shorts
RewriteRule ^index\.php$ http://www.stonemenswear.co.uk/menswear/shorts? [R=301,N]
(plus the rest of the rewrites in the same format)
Each of these are getting redirected to the flip flops page!
Thanks in advance.
Wrong use of flag N you need L flag instead. Replace your code with this:
RewriteCond "%{QUERY_STRING} search=flip flops" [NC]
RewriteRule ^store/index\.php$ http://www.stonemenswear.co.uk//menswear/flip-flops/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^search=Boss\+Orange\+Shorts(&|$) [NC]
RewriteRule ^store/index\.php$ http://www.stonemenswear.co.uk/menswear/shorts/? [R=301,L,NC]
You have some small syntax errors here.
RewriteCond %{QUERY_STRING} "^search=flip flops$" [NC]
RewriteRule ^store/index\.php$ http://www.stonemenswear.co.uk/menswear/flip-flops/? [R=301,L]
RewriteCond %{QUERY_STRING} ^search=Boss\+Orange\+Shorts$ [NC]
RewriteRule ^store/index\.php$ http://www.stonemenswear.co.uk/menswear/shorts/? [R=301,L]

Categories