Mod rewrite - PHP - php

I use urls like
www.example.com/viewpost.php?TITLE=mp3playerforsale&ID=123
Where i am passing ID parameter and Title and then display records from sql in PHP.
Later I can able to use MOD REWRITE functionality in .htaccess and able to convert as dynamic seo friendly url like
www.example.com/mp3playerforsale-123
www.example.com/carforsale-124
Where 123 gets corresponding records stored in sql database, e.g.
ID Title
123 mp3playerforsale
124 carforsale
Now I have the requirement that I have two pages which have ID and title as parameters. But both has to displayed in two different pages.
Example:
mp3players should go to page viewpost.php?ID=123
cars should go to page viewpost2.php?ID=123
When i tried both displayed in viewpost.php where I need to display cars category in different pages.
I am already using this
RewriteEngine On
RewriteRule ([-A-Za-z0-9]+)-([0-9]+)/? viewpost.php?TITLE=$1&ID=$2 [NC]
Any help appreciated !

You may redirect both cases to the single page, where it will be decided which template to use by php code.

I'm not sure i understand your question i hope these will help you
RewriteEngine On
RewriteRule mp3players-([0-9]+)/? viewpost.php?TITLE=mp3players&ID=$1 [NC]
RewriteRule cars-([0-9]+)/? viewpost2.php?TITLE=cars&ID=$1 [NC]
Or this?
RewriteEngine On
RewriteRule mp3players/([-A-Za-z0-9]+)-([0-9]+)/? viewpost.php?TITLE=$1&ID=$2 [NC]
RewriteRule cars/([-A-Za-z0-9]+)-([0-9]+)/? viewpost2.php?TITLE=$1&ID=$2 [NC]
in this your links must have a mp3players or cars prefix
like
www.example.com/mp3players/nameofproduct-123
www.example.com/cars/nameofproduct-124

Related

htaccess file to use forward slash instead of ?, = and & signs in query string?

I am creating a small e-commerce website. There we have a list of items which user can click and explore the information about the item. I am passing the item id and and some other information in the query string so on the next page I can get the related data from database.
Right now we have the following URL default structure:
www.somedomain.com/item.php?id=1&some1=somevalue&some2=somevalue
But I want the URL like:
www.somedomain.com/item/1/somevalue/somevalue
Where item is item.php and /1/somevalue/somevalue is query string ?id=1&some1=somevalue&some2=somevalue. I want my whole website query string parameters like above.
I am using core PHP, no framework. I did some with .htaccess files but the server gives errors while loading the website. How can I do this with .htaccess file?
Try with below, I am using mod_rewrite to achieve the result, clear cache if using any previous attempts.
Below will also cover '_' underscore and next rule after will be for only one item.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z\_]+)/([0-9]+)/([a-zA-Z]+)/([a-zA-Z]+)$ $1.php?id=$2&some1=$3&some2=$4 [L]
RewriteRule ^([a-zA-Z\_]+)/([0-9]+)$ $1.php?item=$2[L]

htaccess redirect to remove duplicate pages

I'm very new to this thing with htaccess rules.
I basically want two things.
Redirect exampledomain.com to www.exampledomain.com. This works, the web host that is used had a FAQ page where they suggested the code I use. What doesn't work is this:
Remove the way to access the "example" page from both "/index.php?p=example" and "/?p=example" to get better search engine ranks. If this means accessing that page from something like "/example", that's a bonus.
I basically want to remove the duplicate links that is caused by the /index.php-variant to get higher search engine ranks. Having prettier links isn't required, just a bonus, the most important thing is that I need to remove the duplicate links.
Here's the code anyway:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^exampledomain.com$
RewriteRule ^(.*)$ http://www.exampledomain.com$1 [R=301]
RewriteRule ^([^/]*)\.html$ /index.php?p=$1 [L]
RewriteRule ^([^/]*)\.html$ /?p=$1 [L]
Have I misunderstood the [L/R=301/*]-flags totally?
EDIT: Ok, to clarify. I want to remove duplicate urls to get higher search engine ranks. What my code above apparently did was to add yet another url to access the same page. Since I only want one way, is there any way to redirect instead of just pointing the "prettified" link to the original?
When I think about it, would a redirection of exampledomain.com/index.php to exampledomain.com do the trick?
Thanks guys so far, sorry for being bad at explaining. :)

Using redirect 301 or rewriterule in htaccess to redirect and take a php variable with you

I think my question is quite simple but I've been banging my head against the wall for the past few hours.
I have my website using rewriterule to ensure messy path names with php variables are now nice and tidy(I've removed [http://www] from my examples because the system thinks I am putting links and won't let me).
So somebody comes to my site at mysite.co.uk/my-product-P1.html the website will know to post mysite.co.uk/product.php?id=1 to the server.
But I also want to tidy it up the other way around. If an old customer or an old link uses the pathname mysite.co.uk/product.php?id=1 then I want it to return mysite.co.uk/my-product-P1.html instead even though the old pathname will actually still work. I don't want customers accessing the same page from different pathnames.
How do I do this and will it create a loop? On another website I have it working using:
RewriteCond %{QUERY_STRING} ^id=1$ [NC]
RewriteRule ^product\.php$ product-P1.html? [R=301,L]
But on that site there are only around 10 products so I'm able to write these lines for each products. On my other site I have hundreds of products so this isn't practical and I need to do it automatically.
Hopefully this makes sense. I have read through other posts and can't find my solution so apologies if this is clearly explained somewhere else.
How do I do this and will it create a loop?
The rules that you have (on the site with 10 products) need to match against the actual request as opposed to the URI:
RewriteCond %{THE_REQUEST} \ /product\.php\?id=([^\ &]+)
RewriteRule ^ /product-P%1.html? [R=301,L]
But you're better off doing this in your php script rather than enumerating all the products in the htaccess file:
On my other site I have hundreds of products so this isn't practical and I need to do it automatically
You can't do that using only mod_rewrite. You'll need to script that in your product.php script. The product.php script will need to check the $_SERVER'[REQUEST_URI'] php variable, and see if it starts with: /product.php.
If it does, then you know someone accessed the php script directly, and you'll need to fetch the product name using the id passed in $_GET['id'], then redirect to the product name + "-P$_GET['id'].html".
The htaccess file and mod_rewrite won't know the mapping between product IDs and product names, so you need to do this in your php script (which does have access to this mapping).
You need this additional rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+product\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /product-P%1.html? [R=302,L]

Multiple htaccess re-write rules with friendly URLs

My mobile site (tiny custom CMS inspired by wordpress) has a homepage, blog page, a news page, an about page and a contact page where ./blog ./news ./about and ./contact are actual folders. Homepage, Blog and News are paginated. About and Contact are single pages, no pagination.
I need htaccess rewrite rules to do the following (A) and (B). I have only made progress with (A), and the htaccess file is in the site root.
(A)
m.website.com/index.php?page=1 re-writes to m.website.com
m.website.com/index.php?page=2 re-writes to m.website.com/page/2 etc
m.website.com/blog/index.php?page=1 rewrites to m.website.com/blog
m.website.com/blog/index.php?page=2 re-writes to m.website.com/blog/page/2 etc
m.website.com/news/index.php?page=1 rewrites to m.website.com/news
m.website.com/news/index.php?page=2 re-writes to m.website.com/news/page/2 etc
Problems:
Below is what I'm using for the above, but I only got it working for the homepage for now. I don't know how to combine the rules to include blog and news pages too. Also, it duplicates my links because m.website.com and m.website.com/page/1 are both in use. I need to get rid of /page/1 everywhere. Pagination should start only from page 2. I tried to get rid of it using the RedirectMatch but it didn't work so I commented it out.
RewriteEngine On
RewriteBase /
RewriteRule ^page/(.*) index.php?page=$1 [NC]
#RedirectMatch 301 ^/page/1/$ http://m.website.com/
(B)
I already have a permalink.php file which accepts pretty URLs and returns their postIDs
The read-more link for each article on the home, blog or news page will have the format http://m.website.com/2012/03/the-post-title
When clicked, the htaccess will query permalink.php with the string /2012/03/the-post-title to get the postID, then opens http://m.website.com/article.php?id=postID but the address in the address bar will be http://m.website.com/2012/03/the-post-title and this shows the article in full, be it home page, blog page or news page.
Problem: I have been searching and I'm not exactly sure how to go about (B) above but I know it's possible. In the end, all rules for A and B will be in the same htaccess file in the site root.
Thanks
This should do the trick for you. It looks like you have done most of it so there isn't muc to do.
RewriteEngine On
RewriteBase /
# you do not need a rewrite for the root as you should have index.php
# as your default document and page 1 as your default page
RewriteRule ^page/1/?$ / [NC,L,R=301]
RewriteRule ^page/(\d+)/?$ index.php?page=$1 [NC,L]
# you do not need a rewrite for blog as you should have index.php as your
# default document and page 1 as your default page
# you do not need a rewrite for news as you should have index.php as your
# default document and page 1 as your default page
RewriteRule ^(blog|news)/page/1/?$ $1 [NC,L,R=301]
RewriteRule ^(blog|news)/page/(\d+)/?$ $1/index.php?page=$2 [NC,L]
######################################################################
################## ADD YOUR PERMALINK.PHP CODE HERE ##################
######################################################################
UPDATE
To effectively turn /2012/03/the-post-title into a postID you need to be able to ask your database to do that for you as it is the only thing that knows the answer. So you can either use a RewriteMap http://httpd.apache.org/docs/2.3/rewrite/rewritemap.html#dbd I have never done this myself and I would advise against it. Not because I know there is a problem with it I just have a bad feeling about it.
The alternative and one I would champion is to just do something like this:-
RewriteRule ^(\d{4})/(\d{2})/([^/]+))/?$ article.php?postIdentifier=$1/$2/$3 [L]
Then in article.php hit the database and use the information in the postIdentifier to get the correct article.
Make sense?

How to load Hard Coded Pages with CMS values using PHP?

Let me explain the scenario first.
I have built a cms in which I load every page on one page like this.
http://www.mywebsite.com/index.php?page=about
Here "about" is a slug of the page I create from the Cms.
So the sql query fetch the record using the slug & retrieve the content of the page & print it on index.php
Now the next thing I have done is, I have done URL rewriting in .htaccess file like below
Options +FollowSymlinks RewriteEngine On
RewriteCond %{REQUEST_URI} !^.(images/|.js|.css).$ [NC]
RewriteRule ^([a-zA-Z0-9_-]+)$ /index.php?page=$1
So now I can use this kind of URLs on my site for same "about" page
http://www.mywebsite.com/about
Now the Question is, these all pages are simply content based, but how should I maintain Dynamic pages.
E.g. I have a page for Member Registration as signup.php
I want to assign all the title, heading, meta desc, meta keywords from CMS to this page but also want to load this page using
http://www.mywebsite.com/signup.php
& not like
http://www.mywebsite.com/signup
What kind of logic should I apply to load the page as signup.php & also fetch the CMS values ?
One solution I have in mind is, we should put one more field in CMS as "Code File :" & enter signup.php over there. But still I am not sure.
Add another rewrite condition which forces it to not redirect to index.php:
RewriteCond %{REQUEST_URI} !\.php$ [NC]

Categories