Dynamic URL rewriting with .htaccess - not rewriting - php

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

Related

Rewrite Url at my website but my index on sub folder

I've been crawling forums for about 2 hours and still haven't found the solution to my problem so I am turning to you guys for help.
My URL looks like this
http://sevalinmutfagi.com/list.php?id=1?kategori=borekler
I want it to look like this
http://sevalinmutfagi.com/tarifler/borekler
I try so much rewrite example but my index file at a subfolder in /View/ so I can't rewrite my URLs
here my .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?sevalinmutfagi.com$ [NC]
RewriteCond %{REQUEST_URI} !^/View/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /View/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?sevalinmutfagi.com$ [NC]
RewriteRule ^(/)?$ View/index.php [L]
This code works well for redirect subfolder all pages but I try to add rewrite URL codes can't work. I hope that has been revealing. Thanks to every for help.
that's worked for me fine.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ /subfolder/$1 [NC,L]
The FIRST thing to learn about mod_rewrite is when NOT to use it. Since mod_alias handles your specific task very nicely, that’s what I’d use:
Redirect 301 / /subfolder/
Since that looks like it would be “loopy,” you could fall back on mod_rewrite:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^subfolder/
RewriteRule .? subfolder%{REQUEST_URI} [R=301,L]
There’s just too much wrong with your attempt to cover … and I’ve addressed exactly those issues many times in many threads. Therefore, you might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It’s helped may members and should help you, too

Simple mod rewrite query string but still failed

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]

phpDolphin SEO Urls

I'm currently using phpDolphin as my website social network, phpDolphin is not SEO URL ready, I was researching on how to fix the .htaccess for friendly seo urls and I cannot seem to deal with it. Here's my current .htaccess
Also here's how urls appear
http://feisbu.me/index.php?a=profile&u=cubaton3
and I want to show: http://feisbu.me/cubaton3
Here's my .htaccess file
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
Please help me, thanks.
A few obvious things:
Mod_rewrite does not change the links you output. Your server side script needs to do that.
Your conditions and rules are kind of a mess, and you clearly need to hardwire your a=profile parameter. Remember that you're matching on, and rewriting the query string unless you specifically are using hostname variables, but they should not be relevant to matching, and are not part of the rewriting process unless you're issuing 301's.
This is typically done with something like this:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-l
RewriteRule ^/(.*)$ /index.php?a=profile&q=$1 [L,QSA]
I'm not familiar with phpDolphin or other routes that this might interfere with, so you will probably need other rules. I personally am not a fan of these "if nothing default route to users" type of setups. Something like "/user/name" would be a cleaner route that will not interfere with other routes, since you can exact match on the parameters.
This is the solution, hope I helped, my site is gofinder.org:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ index.php?a=profile&u=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L,QSA]

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

Remove file extention from urls

I have some urls in my website like this..
http://www.mydomain.com/about.php
http://www.mydomain.com/contact.php
http://www.mydomain.com/signup.php
http://www.mydomain.com/testimonials.php ...... and much more
Now I am trying to convert above urls to user friendly url
http://www.mydomain.com/about
http://www.mydomain.com/contact
http://www.mydomain.com/signup
http://www.mydomain.com/testimonials
This is code in my .htaccess file that I am tried so far. But it doesn't work.
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
Anyone can help me to remove .php etension from my urls??
Thank you.
Get rid of the conditions you are using and instead use the below 2 lines in your htaccess file
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^whatever/?$ whatever.php [NC]
This will work even if user types a forward slash after the name or not
If you want to make them dynamic, here's a good tutorial on that
for a proper implementation so as not cause confusion during deployment if you dont have access to the htaccess file of your server or if it is override. You can consider a library like toro-php http://toroweb.org/ for easy routing.

Categories