how to create pretty urls - php

I have a URL from my website like this -
http://www.mydomain.com/profiles/centers/index.php?code=2507&name=Tharanga+Higher+Educational+Institute#page=art-section
I need to display above url as friendly url on my browser's address bar when it going to above page.
My expecting url something like this -
http://mydomain.com/centers/Tharanga-Higher-Educational-Institute#page=art-section
This code so far in my .htaccess file
# Enable Rewriting
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profiles/centers/index.php\?code=([0-9]+)&name=([^&]+)&?([^\ ]+)
RewriteRule ^profiles/centers/index\.php /%1/?%2 [R=301,L,NE]
But this code is not working for me. hope someone will help me out.
Thank you.

Replace your code with this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+profiles/centers/index\.php\?code=([^&]+)&name=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/([^/]+)/?$ /profiles/centers/index.php?code=$1&name=$2 [L,NC,QSA,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpe?g|gif|png)$ /profiles/centers/%{REQUEST_URI} [NC,L,R=302]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Related

htaccess Multiple URL Variable clean URL

Ok i've tried multiple versions of code on my .htaccess file and I still can't get it to work properly... I don't know if I'm putting it in the wrong folder or what is going on but, here is what I'm trying to accomplish.
This is my code
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} /index\.php\?zipcode=([^\s&]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([a-z0-9]+)/?$ index.php?zipcode=$1&location=$2 [L,QSA,NC]
Here is another version that I've tried
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?zipcode=([a-z0-9]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /index.php?zipcode=$1&location=$2 [QSA,L,NC]
Here is what I'm trying to accomplish... a URL that looks like this:
example.com/rentals/32746/florida
The "32746" and the "florida" come from a form on the index page of the site and then it gets passed over to the "rentals" folder through the URL.
The .htaccess code sorta works whereas it spits out a URL like this:
http://www.example.com/rentals/12345/arkansas?zipcode=12345&location=arkansas
But do you see the extra on the tail end? It's as if it's duplicating the results in the URL.
I currently have the .htaccess file in my "rentals" folder should it be in the root folder?
UPDATED VERSION
For those who were running into the same problem as myself... here is the actual code I ended up going with:
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} /index\.php\?zipcode=([^\s&]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?zipcode=$1&location=$2 [L,QSA,NC]
You need to add a ? to the end of the target path in your first Rule :
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?zipcode=([a-z0-9]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /index.php?zipcode=$1&location=$2 [QSA,L,NC]
As it descards the orignal query strings from the destination.
Clear your browser's cache before testing this.

SEO friendly URL instead of query string .htaccess

How can I modify the .htaccess file and get SEO friendly URLS instead of a query string. I want to achieve this 3 goals:
localhost/example/products/ instead of
localhost/example/products-list.php
localhost/example/products/38/ instead of
localhost/example/products.php?id=38
localhost/example/products/38/red/ instead of
localhost/example/products.php?id=38&color=red
On another post #anubhava helped me a lot and this is what I have right now for the second point:
RewriteEngine on
RewriteBase /example
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ products/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([^/]+)/?$ products.php?id=$1 [L,QSA]
The second point works properly but I want to know where do I have to put the other rules, before or after. I put this right after the other rule, but it's not working because it redirects to the previous url localhost/example/products/38/:
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+)\?color=([^\s&]+) [NC]
RewriteRule ^ products/%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([^/]+)/([^/]+)/?$ products.php?id=$1&color=$2 [L,QSA]
You need new set of rules for 2 parameters:
RewriteEngine on
RewriteBase /example/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+)&color=([^\s&]+) [NC]
RewriteRule ^ products/%1/%2/? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+)\s [NC]
RewriteRule ^ products/%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal forward from pretty URL to actual one
RewriteRule ^products/([^/]+)/?$ products.php?id=$1 [NC,L,QSA]
RewriteRule ^products/([^/]+)/([^/]+)/?$ products.php?id=$1&color=$2 [NC,L,QSA]
RewriteRule ^products/?$ products-list.php [L,NC]

.htaccess mod_rewrite friendly url

I have the php file page.php.
URLS:
page.php?content=contact
page.php?content=aboutus
I need it to be shown like this:
mysite.com/contact
mysite.com/aboutus
I figured out that I have to make .htaccess
RewriteEngine on
RewriteRule ^paginas/([^/]+)/?$ page.php?page=contact [L]
Tryed many things.. No results..
Thanks for attention
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(?:GET|HEAD)\ /+page\.php\?content=([^&\ ]+)
RewriteRule ^ /%1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /page.php?content=$1 [L]
This will first redirect: /page.php?content=anything to /anything then internally rewrite back requests like /anything to /page.php?content=anything.

How to exclude main directory/home page from rewriting rule in htaccess?

I can not figure out how to exclude home page from rewriting.
In other words, .htaccess treats "example.com" as "example.com/user/index.php?name=" so it displays user specific content instead of my main page. I've tried bunch of things and nothing worked as I expected.
RewriteEngine on
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^about/([A-Za-z0-9-]+)/?$ /about/index.php?p=$1 [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteCond %{REQUEST_URI} !^/login/?$ [NC]
RewriteCond %{REQUEST_URI} !^/register/?$ [NC]
RewriteCond %{REQUEST_URI} !^/about/?$ [NC]
RewriteRule ^([A-Za-z0-9.]+)/?$ /user/index.php?name=$1 [L]
Hopefully you guys understand what my problem is.
Rules look fine, the + qualifier in your regex should make it so it won't match just the / request. You can maybe try adding this right below RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+(?:index\.php|)($|\ )
RewriteRule ^ - [L]

Rewrite php page with htaccess

I am creating a form at poll.php?code=STRINGHERE
I want to rewrite it with .htaccess to do WEBSITE.com/STRINGHERE
I have done it before but don't remember what I need to do in the .htaccess its something like
RewriteEngine On
RewiteRule ^(\w.+)$ ./poll.php?code=$1
Although I might be wrong. Any help would be great.
Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewiteRule ^([^/]+)$ ./poll.php?code=$1 [L,QSA]
And if you need to redirect the browser when requests are made directly to poll.php:
RewriteCond %{THE_REQUEST} \ /+poll\.php\?code=([^&\ ]+)
RewriteRule ^ /%1? [L,R]

Categories