Adjusting dynamically generated links using .htaccess or any other method? [duplicate] - php

This question already has answers here:
Adjusting a php function so that it displays link in the form of:- '/widgets?pg=2' instead of 'products.php?cat=20&pg=2'
(2 answers)
Closed 2 years ago.
I have an e-commerce website (created with a ecommercetemplates shopping cart PHP template) and on the catalogue pages there are, at times, a few pages which are numbered at the bottom. The links to these pages are, for example, in the form:- /product.php?cat=27&pg=2 despite the main page having been mapped to:- /widgets using a rewrite rule in a .htaccess file.
For the sake of consistency and simplicity, I would like the pages to be as:- /widgets?pg=2. I asked in this forum how to do this and found the following solution:-
RewriteRule ^widgets$ products.php?cat=27 [QSA]
The problem is that despite the above rewrite rule working as I wanted, the current links on the catalogue page still point to, for example:- /product.php?cat=27&pg=2. In order to go to /widgets?pg=2, I have to enter this in the browser.
So my question is: How do I change the current links on the catalogue page, which are dynamically generated? Are there any further rules which can be entered in .htaccess or is there any other solution to this problem? The following is the PHO code for the product page:- http://freetexthost.com/3ubiydspzm.
In the link above, if you do a search for 'writepagebar', I think this is where the 'next page', 'previous page', and the 'page numbers' are done.

You have a couple of options:
Most of them require more than just .htaccess rewrite rules.
You can modify the writepagebar function to output the friendly URL for you. (And change any other function that might link to this products page.)
You can buffer the entire page's output and do a string replace on it. (See ob_start and ob_get_clean in the PHP manual.)
You can redirect ugly URLs to pretty ones with a 301 redirect. (The links will stay the same, but your friendly URLs get the SEO credit and show up in the browser.)
You can do it any of these ways: How to redirect old "ugly" urls to seo-friendly ones?
Or you can try this ugly mod_rewrite hack that I can't guarantee will work on your server or with your application.
Hack:
RewriteCond %{QUERY_STRING} (.*)(cat=27\&)(.*) [OR]
RewriteCond %{QUERY_STRING} (.*)(cat=27)(.*)
RewriteCond %{THE_REQUEST} !widgets
RewriteRule ^products.php$ widgets?%1%3 [R=301,L]
(This might need the rewrite base option)
http://services.rrbits.com/products.php?cat=27&pg=2 (Example link.)

Related

Dynamic 301 redirects

I'm trying to redirect thousands of ids on a single dynamic PHP and MySQL page. I work for a news website with around 7,000 articles published and my boss decided to change a 10 year old URL to a new one at the end of last year, crazy right!
I have put in redirects from the old to the new site for standard static pages, but it is the dynamic pages that hold 1000's of article ids that has got me in a pickle. I have tirelessly looked for an answer, but to no avail.
For example the below redirect is for just one id. The code below will redirect said specific article. Is there a way to redirect all ids on my MySQL database, without hand coding all of them like below, as this would be impractical and an impossible mission? If not what would be best practice in my situation after a massive website URL change?
RewriteRule ^article\.php$ - [L]
RewriteCond %{QUERY_STRING} ^article_id=224509$
RewriteRule ^securitieslendingnews/article.php/?$ https://www.securitiesfinancetimes.com/securitieslendingnews/article.php?article_id=224509 [L,NE,R=301]
Thank you for any help in advance.
Not tested but something like this ..
RewriteRule ^securitieslendingnews/article.php?article_id=([0-9]+)$ ./securitieslendingnews/article.php?article_id=$1 [L,NE,R=301]

How to get my CMS System work with SEO friendly URLs

i got a question abount seo friendly urls.
Manly, i would like to know whats the best way to get my system running with seo friendly urls. i have programmed my own cms system which is working at the moment with url like this:
/content.php?contentid=ID
On every content page you can change the Meta-Tags.
Every LINK which is generated in my CMS follows this structure. If i want to use SEO friendly URLs changing every link, or code, where a link is generated, might be a bad idea. So i am looking for a nicer way. Is there any commonly used way to handle this?
My first idea was to automatically generate a new .htacces file with ModRewrite after the MetaTags of a Contentfile have been changed. So the SEO friendly URLs are recognized by the system. To get the LINKs on every page working i was thinking about checking the current url and redirecting to the new seo friendly url at the beginning of the content.php file.
I dont know if this is the right way to work with seo friendly urls. How are other CMS Systems handling this issue? I would be thankful for any inspiration!
if you are using an framework(which i think you are) then you can use a .htaccess like this to send the new cleaner links in an $_GET to the framework:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

PHP Site Generating Strange Pages

I have a dynamic PHP based site and I've recently noticed its generating a lot of weird pages like this:
http://www.festivalsnap.com/festival/3151748-16th+Annual+Magnolia+Fest+/hotels/3151748-16th+Annual+Magnolia+Fest+/ticket/hotels
The site architecture should be like this www.mysite.com/festival/ and then there are 4 possible child pages for each event... /lineup /tickets /hotels /news
As you can see from the URL it just keeps creating more and more unwanted child pages. When I run a sitemap generator it will just keep going forever and creating more of these pointless pages.
It shouldn't go any deeper than the /hotels page but for some reason its just adding more and more child pages using any combination of the above pages.
I'm no good with PHP and my developer isnt being very helpful. Anyone know what could be causing this?
Edit:
The main event page comes from a file called festival.php and then there are 4 child pages under that - lineup.php tickets.php hotel.php and news.php that get variables from the event page (event title, dates, location, etc) and use it to search for tickets, hotels, etc.
I have noticed that I can tack on basically anything to the URL and it will add it in as part of the page title/event title. It looks like there is something weird going on with .htaccess
Here is the .htaccess code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.festivalsnap.com$ [NC]
RewriteRule ^(.*)$ http://www.festivalsnap.com/$1 [R=301,L]
RewriteRule festival/(.*)-(.*)/lineup$ lineup.php?eveid=$1&festival=$2
RewriteRule festival/(.*)-(.*)/news$ news.php?eveid=$1&festival=$2
RewriteRule festival/(.*)-(.*)/tickets$ ticket.php?eveid=$1&festival=$2
RewriteRule festival/(.*)-(.*)/hotels$ hotel.php?eveid=$1&festival=$2
RewriteRule festival/(.*)-(.*)/hotels/(.*)$ hotel.php?eveid=$1&festival=$2&hsort=$3
RewriteRule festival/(.*)-(.*)$ event_page.php?eveid=$1&festival=$2
RewriteRule artists/(.*)-(.*)$ artists.php?artid=$1&artname=$2
This is partly something to do with your generator, and partly to do with .htaccess. The .* operator is extremely aggressive, so your .htaccess file says pretty much anything containing festival/ with a hyphen somewhere later in the URL is a valid URL.
But that doesn't explain why your generator is "finding" all of those pages; there must be some bad links being created somewhere, either internally in the generator or in links on pages on your site.
Can you post some code?
EDIT: The .htaccess code should be much narrower - try replacing each of the occurrences of (.*) with ([^/]*).
As for the PHP, it's impossible to say exactly what is going on, but it sounds like the generator is finding those links on your site somewhere and following them, in which case the sitemap generator is working correctly, but your content has problems. Check your logs, find one of the incorrect URLs, and see what page referred the user there. That will tell you where to look for the bad code.

Specific mod rewrite on URL with multiple variables

appreciate help in advance. I'm using mod rewrite and need help in converting specific PHP URLS into prettier URLs.
Specifically, I want to convert the PHP URL:
www.sample.com/subcategory.php?subcategory=sports-achievement&page=1
to
www.sample.com/sports/sports-achievement.html
for the user.
So to be specific, the rule is:
a) the first part of the subategory before the dash should be the
directory (e.g., sports- - into /sports/
b) the entire subcategory as
the page string "sports-achievement.html
c) the PHP pages still needs
to pass the page strings (as there are multiple pages)
Thanks in advance for your help!
To get the the redirect and internal rewrite, you'll have to do something like this.
#Redirect
RewriteCond %{THE_REQUEST} (GET|POST)\ /subcategory\.php\?subcategory=([^-]+)-([^&]+)(&(.*)|)\ HTTP
RewriteRule ^subcategory\.php$ /%2/%2-%3.html?%5 [R,L]
#Internal rewrite
RewriteRule ^(.*)/\1-(.*)\.html$ subcategory.php?subcategory=$1-$2 [QSA,L]
There is no real way of hiding the page name. You can use a post-request and hide it in the post-body. My answer to this question shows how to do it with cookies, but this will most likely confuse search engines, as different pages are somehow available under the same page name.

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]

Categories